ios – 使用animateKeyframesWithDuration在屏幕上创建一个连续旋转的方块

ios – 使用animateKeyframesWithDuration在屏幕上创建一个连续旋转的方块,第1张

概述我尝试使用下面的代码在屏幕上创建一个连续旋转方块.但我不知道为什么转速在变化.如何更改代码以使旋转速度不变?我尝试了不同的UIViewKeyframeAnimationOptions,但似乎没有一个工作. override func viewDidLoad() { super.viewDidLoad() let square = UIView() square.fram 我尝试使用下面的代码在屏幕上创建一个连续旋转的方块.但我不知道为什么转速在变化.如何更改代码以使旋转速度不变?我尝试了不同的UIVIEwKeyframeAnimationoptions,但似乎没有一个工作.
overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let square = UIVIEw()    square.frame = CGRect(x: 55,y: 300,wIDth: 40,height: 40)    square.backgroundcolor = UIcolor.redcolor()    self.vIEw.addSubvIEw(square)    let duration = 1.0    let delay = 0.0    let options = UIVIEwKeyframeAnimationoptions.Repeat        UIVIEw.animateKeyframesWithDuration(duration,delay: delay,options: options,animations: {        let fullRotation = CGfloat(M_PI * 2)        UIVIEw.addKeyframeWithrelativeStartTime(0,relativeDuration: 1/3,animations: {            square.transform = CGAffinetransformMakeRotation(1/3 * fullRotation)        })                                UIVIEw.addKeyframeWithrelativeStartTime(1/3,animations: {            square.transform = CGAffinetransformMakeRotation(2/3 * fullRotation)        })                                UIVIEw.addKeyframeWithrelativeStartTime(2/3,animations: {            square.transform = CGAffinetransformMakeRotation(3/3 * fullRotation)        })        },completion: {finished in        })    }
解决方法 这真的很奇怪… UIVIEw.animateKeyframesWithDuration不能正常工作,因为我希望它与UIVIEwKeyframeAnimationoptions.CalculationModelinear | UIVIEwKeyframeAnimationopti ons.Repeat传入了选项.

如果使用非块方法创建关键帧动画(请参见下文),则旋转将按预期重复.

如果我发现为什么基于块的选项不起作用,我会尝试记住在这里更新答案!

overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    let square = UIVIEw()    square.frame = CGRect(x: 55,height: 40)    square.backgroundcolor = UIcolor.redcolor()    self.vIEw.addSubvIEw(square)    let fullRotation = CGfloat(M_PI * 2)    let animation = CAKeyframeAnimation()    animation.keyPath = "transform.rotation.z"    animation.duration = 2    animation.removedOnCompletion = false    animation.fillMode = kCAFillModeForwards    animation.repeatCount = float.infinity    animation.values = [fullRotation/4,fullRotation/2,fullRotation*3/4,fullRotation]    square.layer.addAnimation(animation,forKey: "rotate")}
总结

以上是内存溢出为你收集整理的ios – 使用animateKeyframesWithDuration在屏幕上创建一个连续旋转的方块全部内容,希望文章能够帮你解决ios – 使用animateKeyframesWithDuration在屏幕上创建一个连续旋转的方块所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105301.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存