2011-08-28 180 views
1

我有一系列CALayers,我正在循環並嘗試移動。移動CALayer的動畫?

瓷磚是CALayer的子類,有一個的CGRect屬性命名originalFrame其中i商店,我想動畫的框架。

當我下面期運用一切代碼瞬間移動到正確的possition並沒有4秒的動畫。我怎樣才能使這些CALayer動畫?

 for (int i = 0; i < [tileArray count]; i++) { 
      [UIView beginAnimations:nil context:NULL]; 
      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
      [UIView setAnimationDelay:i]; 
      [UIView setAnimationDuration:4]; 
      Tile *currentCard = (Tile*)[tileArray objectAtIndex:i]; 
      currentCard.frame = currentCard.originalFrame; 
      [UIView commitAnimations]; 
     } 

回答

3

你有兩個問題:第一個是你想層的frame直接動畫。由於這是一個派生財產,你不能那樣做。相反,你必須動畫position屬性。 http://developer.apple.com/library/mac/#qa/qa1620/_index.html

其次,你使用UIView的+beginAnimations API,但你說你的Tile對象是CALayers,而不是UIViews。所以你不需要使用+beginAnimations。相反,你需要使用CAAnimation對象,如CABasicAnimation(未經測試):

for (Tile *tile in tileArray) 
{ 
    static NSString * const kProperty = @"position"; 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:kProperty]; 
    animation.duration = 4.0f; 
    animation.fromValue = [tile valueForKey:kProperty]; 
    animation.toValue = [NSValue valueWithCGRect:tile.originalFrame]; 
    [tile addAnimation:animation forKey:kProperty]; 
} 
+0

謝謝,exatcly我一直在尋找! – David