2011-04-13 33 views
2

我有一個相當好奇的問題。UIView圖層的animationForKey:始終爲零

我認爲我用CAKeyframeAnimation進行動畫製作。然而,動畫按預期工作,一旦代理的animationDidStop:finished方法被觸發,與圖層的animationForKey:方法一起檢查時,與該視圖相關聯的圖層總是返回nil

這裏的問題的說明:

// This is in the view controller 
- (void) doAnimation:(id)sender { 
    CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 

    [positionAnimation setValues:arrayOfPoint]; 
    [positionAnimation setDuration:0.5]; 
    // ViewController is the delegate and has the animationDidStop:finished: method 
    positionAnimation.delegate = self; 
    [positionAnimation setValue:@"I am Text" forKeyPath:@"positionAnimation"]; 

    [someView.layer addAnimation:positionAnimation forKey:@"positionAnimation"]; 
    someView.layer.position = newPosition; 
} 
控制器

在其他地方,我有animationDidStop:finished:方法定義如下:

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { 

     NSArray *array = [someView.layer animationKeys]; 
     NSString *stringValue = [anim valueForKey:@"positionAnimation"]; 

     NSLog(@"valueForKey: %@", stringValue); 
     // Prints 'I am Text' 

     if(anim == [someView.layer animationForKey:@"positionAnimation"]) { 
      //THIS NEVER GET CALLED 
     } 
} 

爲什麼if塊永遠不會計算爲真?爲什麼[someView.layer animationForKey:@"positionAnimation"]總是nil?此外,stringValue具有正確的數據,但數組也是nil

+0

我看到這個[說明](http://stackoverflow.com/questions/1255086/how-to-identify-caanimation-within-the-animationdidstop-delegate/5010291#5010291 );誰能證實這個理論 - 請嗎? – haroldcampbell 2011-04-13 08:11:07

回答

2

我只能猜測,但也許當方法animationDidStop:finished:被稱爲你的動畫已經從圖層中刪除。所以[someView.layer animationForKey:@"positionAnimation"]是零。

希望這會幫助

+1

謝謝 - 它的確如此。更具體地說,在將動畫添加到圖層之前,我必須調用方法'[positionAnimation setRemovedOnCompletion:NO];'。 所以看起來好像下面的情況一樣:1:當動畫被添加到圖層時,它通過副本完成。 2:動畫完成後,動畫從圖層中移除。 (恕我直言)最好直接在動畫對象上設置KVO,然後在'animationDidStop:finished:'方法中檢查該KV。 – haroldcampbell 2011-04-13 15:15:51