Nix*_*aAK 8 animation rotation repeat ios swift
我尝试使用下面的代码在屏幕上创建一个连续旋转的方块.但我不知道为什么转速在变化.如何更改代码以使旋转速度不变?我尝试了不同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, relativeDuration: 1/3, animations: {
square.transform = CGAffineTransformMakeRotation(2/3 * fullRotation)
})
UIView.addKeyframeWithRelativeStartTime(2/3, relativeDuration: 1/3, animations: {
square.transform = CGAffineTransformMakeRotation(3/3 * fullRotation)
})
}, completion: {finished in
})
}
Run Code Online (Sandbox Code Playgroud)
小智 8
我之前遇到过同样的问题,这就是我的工作方式:
let raw = UIViewKeyframeAnimationOptions.Repeat.rawValue | UIViewAnimationOptions.CurveLinear.rawValue
let options = UIViewKeyframeAnimationOptions(rawValue: raw)
Run Code Online (Sandbox Code Playgroud)
在放弃之前,我想出了这个问题.我不认为有关于它的文档,但它只是工作.
这真的很奇怪......UIView.animateKeyframesWithDuration并没有像我期望的那样工作,并UIViewKeyframeAnimationOptions.CalculationModeLinear|UIViewKeyframeAnimationOpti\xe2\x80\x8c\xe2\x80\x8bons.Repeat通过选项传递。
如果您使用非块方法创建关键帧动画(见下文),旋转将按预期重复。
\n\n如果我发现为什么基于块的选项不起作用,我会尝试并记住在这里更新答案!
\n\noverride func viewDidLoad() {\n super.viewDidLoad()\n\n\n let square = UIView()\n square.frame = CGRect(x: 55, y: 300, width: 40, height: 40)\n square.backgroundColor = UIColor.redColor()\n self.view.addSubview(square)\n\n let fullRotation = CGFloat(M_PI * 2)\n\n let animation = CAKeyframeAnimation()\n animation.keyPath = "transform.rotation.z"\n animation.duration = 2\n animation.removedOnCompletion = false\n animation.fillMode = kCAFillModeForwards\n animation.repeatCount = Float.infinity\n animation.values = [fullRotation/4, fullRotation/2, fullRotation*3/4, fullRotation]\n\n square.layer.addAnimation(animation, forKey: "rotate")\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3190 次 |
| 最近记录: |