2015-04-04 69 views
0

我正在使用CAKeyframeAnimation將CALayer移動到圓形軌跡上。有時我需要停止動畫並將動畫移動到動畫停止點。這裏代碼:CAKeyFrameAnimation表示層似乎不會改變

CAKeyframeAnimation* circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
CGMutablePathRef circularPath = the circle path; 

circlePathAnimation.path = circularPath; 
circlePathAnimation.delegate = self; 
circlePathAnimation.duration = 3.0f; 
circlePathAnimation.repeatCount = 1; 
circlePathAnimation.fillMode = kCAFillModeForwards; 
circlePathAnimation.removedOnCompletion = NO; 
circlePathAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:.43 :.78 :.69 :.99]; 
[circlePathAnimation setValue:layer forKey:@"animationLayer"]; 
[layer addAnimation:circlePathAnimation forKey:@"Rotation"]; 


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
    CALayer *layer = [anim valueForKey:@"animationLayer"]; 
    if (layer) { 
     CALayer *presentationLayer = layer.presentationLayer; 
     layer.position = presentationLayer.position; 
    } 
} 

但是表示層沒有位置變化!!!我讀到它從ios 8中不再可靠。那麼有沒有其他方法可以找到動畫層的當前位置?

+1

我已經獲得了在iOS 8中使用表示層的動畫對象的位置(實際上,frame.origin),並且它似乎工作正常。你能提供一個你讀到的地方的參考,它不再可靠嗎?我用presentationLayer.position對它進行了測試,結果也起作用。 – rdelmar 2015-04-05 00:24:42

回答

1

我不知道表示層的位置屬性停止告訴你在iOS 8中你的圖層的位置。這很有趣。在表現層上使用hitTest方法進行命中測試仍然有效,但我只是在一個我後來寫過的應用程序上嘗試過。

暫停和恢復動畫也仍然有效。

對於像圓形路徑這樣的簡單路徑,您可以檢查動畫的時間,然後使用trig來計算位置。

對於更復雜的路徑,您必須瞭解圖層遍歷的路徑。隨着時間的推移繪製單個三次或二次貝塞爾曲線非常簡單,但複雜的UIBezierPath/CGPath混合了不同的形狀,這將是一個難以解決的問題。