2015-10-14 83 views
1

嗨我正在使用CABasicAnimation在CAShapeLayer中設置動畫路徑。當用戶TAPP的按鈕,我叫:CABasicAnimation - 從一個動畫切換到另一個動畫。如何獲得動畫的當前值?

CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; 
    expandAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath; 
    expandAnimation.duration = 1.0; 
    expandAnimation.fillMode = kCAFillModeBoth; 
    expandAnimation.removedOnCompletion = NO; 
    [self.circleShape addAnimation:expandAnimation forKey:expandAnimation.keyPath]; 

當釋放按鈕,我動畫這樣的:

CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"path"]; 
    narrowAnimation.fromValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath; 
    narrowAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, 20, 20)].CGPath; 
    narrowAnimation.duration = .5; 
    narrowAnimation.fillMode = kCAFillModeBoth; 
    narrowAnimation.removedOnCompletion = NO; 
    [self.circleShape addAnimation:narrowAnimation forKey:narrowAnimation.keyPath]; 

有時第一動畫還是動畫,同時我開始第二個。我怎樣才能得到第一個動畫的當前狀態,所以我可以設置第二個動畫的起始值從該狀態開始?

對於使用BezierPaths的值。

+0

你嘗試'[層removeAnimationForKey:@ 「路徑」];'做一個動畫之前? – anhtu

+0

這不是我想的問題。當我調用第二個動畫時,第一個動畫會自動停止。我只想讓這個圈子從當前狀態開始縮小,而不是從最大尺寸縮小。 –

+0

謝謝,我現在明白了這個問題。 – anhtu

回答

1

您可以使用presentationLayer,它返回表示當前出現在屏幕上的圖層狀態的表示層對象的副本。

因此,通過使用self.circleShape.presentationLayer,您將獲得當前顯示在屏幕上的圖層。

編輯: 所以解決的辦法是

narrowAnimation.fromValue = [self.circleShape.presentationLayer valueForKeyPath:@"path"]; 
+0

我認爲這是要走的路。但我怎樣才能訪問路徑屬性? –

+0

hm ..你可以得到它的位置,並從那個位置開始第二個動畫 – l0gg3r

+0

CAShapeLayer有一個'path'屬性,是不是你需要的? – l0gg3r

相關問題