[英]swift stop 360 degree animation
在我的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度后)將停止-但這太晚了。
按下按鈕后,圖像必須停止直接在實際位置上旋轉
當按下停止動畫的按鈕時,嘗試添加以下內容:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.