在我的swift 2应用程序中,我有这个扩展名:
extension UIView {
func rotate360Degrees(duration: CFTimeInterval = 2.5, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2.0)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate
}
self.layer.addAnimation(rotateAnimation, forKey: nil)
}
}
使用此代码,我可以将图像旋转360度。 现在我想按下按钮后直接停止这个动画。
在我的视图中,控制器是我按钮的动作。如果按此按钮,将设置以下值:
self.shouldStopRotating = true
我也将此代码部分放在同一个vc中:
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if self.shouldStopRotating == false {
self.LoadingCircle.rotate360Degrees(completionDelegate: self)
}
}
按下按钮后图像将停止,但动画完成后(360度后)将停止 - 但现在已经很晚了。
按下按钮
后,图像必须在实际位置上停止旋转答案 0 :(得分:0)
尝试在停止动画的按钮时添加此项:
self.LoadingCircle.layer.removeAllAnimations()
let currentLayer = self.LoadingCircle.layer.presentationLayer();
let currentRotation = currentLayer?.valueForKeyPath("transform.rotation.z")?.floatValue;
let rotation = CGAffineTransformMakeRotation(CGFloat(currentRotation!));
self.LoadingCircle.transform = rotation;