2016-02-26 54 views
0

,我有這個擴展名:迅速停止360度的動畫

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

不能看到你在哪裏代碼停止了。檢查這個[回答](http://stackoverflow.com/a/1567855) – zcui93

+0

按下按鈕後,我調用的唯一的代碼部分是這樣的:self.shouldStopRotating = true,但我認爲這我不足以停止動畫直接。現在我正在尋找解決方案。 – GhostStack

+0

在我的鏈接答案'removeAnimationForKey'應該是正確的電話 – zcui93

回答

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; 
+0

不。旋轉360度後首先停止 – GhostStack