在ios中使用关键帧动画时如何设置动画曲线?

 tcystars_628 发布于 2023-02-09 19:08

使用UIView的关键帧动画时如何设置动画曲线:

animateKeyframesWithDuration:delay:options:animations:completion:

无论我在动画块中做什么似乎是线性的(除非我使用该UIViewKeyframeAnimationOptionCalculationModeCubic选项,但这不是我想要的).

我想在动画中UIViewAnimationOptionCurveEaseOut使用缓动曲线,就像使用常规动画时的选项一样:

animateWithDuration:delay:options:animations:completion:

eskimwier.. 22

Swift 2.0不允许使用UIViewAnimationOptionsas作为UIViewKeyframeAnimationOptions.它也不允许|它们在一起.

但是,有一个初始化UIViewKeyframeAnimationOptions程序需要一个rawValue.所以,下面的代码工作投入UIViewAnimationOptionsUIViewKeyframeAnimationOptions:

let animationOptions: UIViewAnimationOptions = .CurveEaseOut
let keyframeAnimationOptions: UIViewKeyframeAnimationOptions = UIViewKeyframeAnimationOptions(rawValue: animationOptions.rawValue)

然后我可以keyframeAnimationOptions转到animateKeyframesWithDuration,一切都很好.

4 个回答
  • 这是我想出的一个漂亮而干净的Swift解决方案:

    extension UIViewKeyframeAnimationOptions {
    
        init(animationOptions: UIViewAnimationOptions) {
            rawValue = animationOptions.rawValue
        }
    
    }
    

    用法:

    UIViewKeyframeAnimationOptions(animationOptions: .CurveEaseOut)
    

    2023-02-09 19:10 回答
  • 通过@eskimwier答案试图曲线设置时并没有为我工作linearanimateKeyframes(withDuration:delay:options:animations:completion:)。这是在Swift 3的Xcode 8.2.1中。

    我的动画似乎默认为easeInOut(启动速度慢,中间速度快,结尾速度慢)。苹果是否更改了默认曲线?

    无论如何,我想要一条线性曲线,并尝试了以上建议的方法UIViewAnimationOptions与关键帧动画配合使用:

    let animationOptions: UIViewAnimationOptions = .curveLinear
    var keyframeAnimationOptions: UIViewKeyframeAnimationOptions = UIViewKeyframeAnimationOptions(rawValue: animationOptions.rawValue)
    
    UIView.animateKeyframes(withDuration: 3, delay: 0, options: [keyframeAnimationOptions], animations: {
        //... animations here ...
    }, completion: nil)
    

    这没有效果。对我唯一有用的是在调用animateKeyframes之前执行此操作:

    UIView.setAnimationCurve(UIViewAnimationCurve.linear)
    

    2023-02-09 19:10 回答
  • 实际上,您可以使用与animateWithDuration相同的曲线标志:delay:options:animations:completion:.只需"二进制"或"与animateKeyframesWithDuration调用中的其他选项":delay:options:animations:completion:

    文档令人困惑,但确实有效.

    您可以使用的曲线值是:

    UIViewAnimationOptionCurveEaseInOut = 0 << 16,
    UIViewAnimationOptionCurveEaseIn    = 1 << 16,
    UIViewAnimationOptionCurveEaseOut   = 2 << 16,
    UIViewAnimationOptionCurveLinear    = 3 << 16,
    

    您获得的默认动画曲线是0或缓入,缓出.

    2023-02-09 19:10 回答
  • Swift 2.0不允许使用UIViewAnimationOptionsas作为UIViewKeyframeAnimationOptions.它也不允许|它们在一起.

    但是,有一个初始化UIViewKeyframeAnimationOptions程序需要一个rawValue.所以,下面的代码工作投入UIViewAnimationOptionsUIViewKeyframeAnimationOptions:

    let animationOptions: UIViewAnimationOptions = .CurveEaseOut
    let keyframeAnimationOptions: UIViewKeyframeAnimationOptions = UIViewKeyframeAnimationOptions(rawValue: animationOptions.rawValue)
    

    然后我可以keyframeAnimationOptions转到animateKeyframesWithDuration,一切都很好.

    2023-02-09 19:10 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有