2016-09-17 65 views
0

我有一個CAShapeLayer,其strokeEnd我偶爾更新一個NSTimer循環內。當我打電話時,效果很好:動畫CAShapeLayer:完成時做

myPathLayer.strokeEnd += 0.1 

此路徑會自動改變爲我想要的動畫。我的問題是,因爲這不是函數調用,所以沒有完成塊。一旦這個動畫完成,我想執行另一個動作。我怎樣才能做到這一點?

回答

1

您可以使用CATransaction來包裝以設置圖層動畫的完成塊。

CATransaction.begin() 
CATransaction.setCompletionBlock({self.myFunction()}) 
myPathLayer.strokeEnd += 0.1 
CATransaction.commit() 

希望有更好的辦法,但這是我做到的。

+0

這是在我的情況完全精解。謝謝。 – Connor

0

您可以使用CAAnimation委託方法。

class Whatever: CAAnimationDelegate { ... 

然後

let animation = CABasicAnimation(keyPath: <your_key_path>) 
... 
animation.delegate = self 

,並使用委託方法:

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { 

    if flag { <do_what_you_need_to> } 
}