开始/停止图像视图旋转动画 [英] Start/stop image view rotation animations
本文介绍了开始/停止图像视图旋转动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个开始/停止按钮和一个要旋转的图像视图.
I have a start/stop button and an image view which I want to rotate.
当我按下按钮时,我要开始旋转,而当我再次按下按钮时,图像应停止旋转.我目前正在使用UIView
动画,但是我还没有想出一种方法来停止查看动画.
When I press the button I want the to start rotating and when I press the button again the image should stop rotating. I am currently using an UIView
animation, but I haven't figured out a way to stop the view animations.
我希望图像旋转,但是当动画停止时,图像不应回到起始位置,而应继续动画.
I want the image to rotate, but when the animation stops the image shouldn't go back to the starting position, but instead continue the animation.
var isTapped = true
@IBAction func startStopButtonTapped(_ sender: Any) {
ruotate()
isTapped = !isTapped
}
func ruotate() {
if isTapped {
UIView.animate(withDuration: 5, delay: 0, options: .repeat, animations: { () -> Void in
self.imageWood.transform = self.imageWood.transform.rotated(by: CGFloat(M_PI_2))
}, completion: { finished in
ruotate()
})
} }
这是我的代码,但无法按我的方式工作.
It is my code, but it doesn't work like I aspect.
推荐答案
Swift 3.x
开始动画
let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: .pi * 2.0)
rotationAnimation.duration = 0.5;
rotationAnimation.isCumulative = true;
rotationAnimation.repeatCount = .infinity;
self.imageWood?.layer.add(rotationAnimation, forKey: "rotationAnimation")
停止动画
self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")
Swift 2.x
开始动画
let rotationAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(double: M_PI * 2.0)
rotationAnimation.duration = 1;
rotationAnimation.cumulative = true;
rotationAnimation.repeatCount = .infinity;
self.imageWood?.layer.addAnimation(rotationAnimation, forKey: "rotationAnimation")
停止动画
self.imageWood?.layer.removeAnimation(forKey: "rotationAnimation")
这篇关于开始/停止图像视图旋转动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文