2010-09-01 16 views
0

我想要通過一組CATextLayers來一個接一個地動畫。層創建並添加在一個循環中。我需要動畫才能像翻書一樣工作。到目前爲止,作爲概念證明,我已經使用了CABasicAnimations,但爲了按順序動畫多個圖層,我認爲我需要其他核心動畫選項之一,但我不確定哪一個。這裏的循環添加子層:如何在覈心動畫子圖層數組中循環並按順序應用動畫

- (void) initLayers: (NSString *)endChar 
{ 
CALayer *rootLayer = self.layer; 
int counter =0; 

for (NSString *element in alphabet) { 
    NSLog(@"element: %@",element); 
    NSString *topLayerName = [NSString stringWithFormat:@"TOP_%d_%@",counter,element]; 
    NSString *bottomLayerName = [NSString stringWithFormat:@"BOT_%d_%@",counter,element]; 
    //get index of endchar - indexOfObject 

    if (element != endChar) { //change to if key value is less than or equal to endchar index value 

     CATextLayer *topLayer = [CATextLayer layer]; 
     topLayer.name = topLayerName; 
     [topLayer setAnchorPoint:CGPointMake(0.5f,1.0f)]; // set the anchorpoint for the transform. This affects layer position too 
     topLayer.bounds = CGRectMake(0.0f, 0.0f, CHARACTER_WIDTH, CHARACTER_HEIGHT/2); 
     topLayer.string = element; 
     topLayer.font = solariFont.fontName; 
     topLayer.fontSize = FONT_SIZE; 
     topLayer.backgroundColor = [UIColor blackColor].CGColor; 
     topLayer.position = CGPointMake(30,30); 
     topLayer.wrapped = NO; 
     [rootLayer addSublayer:topLayer]; 


     CATextLayer *bottomLayer = [CATextLayer layer]; 

     bottomLayer.name =bottomLayerName; 
     [bottomLayer setAnchorPoint:CGPointMake(0.5f,0.0f)]; // set the anchorpoint for the transform. This affects layer position too 

     bottomLayer.bounds = CGRectMake(0.0f,CHARACTER_HEIGHT/4, CHARACTER_WIDTH, CHARACTER_HEIGHT/2); 
     bottomLayer.string = element; 
     bottomLayer.font = solariFont.fontName; 
     bottomLayer.fontSize = FONT_SIZE; 
     bottomLayer.backgroundColor = [UIColor blackColor].CGColor; 
     bottomLayer.position = CGPointMake(topLayer.position.x,topLayer.position.y+2); 
     bottomLayer.wrapped = NO; 

     [rootLayer addSublayer:bottomLayer]; 

     counter++; 
    } 

} 


[self animChars:rootLayer.sublayers]; 
} 

的問題是我怎麼能動畫後,其他各層一個沒有動畫的每一層都在同一時間發生?我希望能夠遍歷子層數組,並依次爲每個CATextLayer設置動畫。我需要CATransactions,MediaTiming嗎?我已經通過核心動畫指南,但沒有比這更明智的了。

回答

0

我通過給每個動畫的關鍵和animationDidStop測試該這樣做:

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

    if (anim ==[topFront animationForKey:@"topCharFlip"]) { .... 
0

實際上這很容易。你首先需要確定你想要的動畫之間的時間間隔。將圖層添加到for循環內的根層時,將動畫媒體定時的屬性beginTime設置爲每次循環觸發時都會遞增的CGFloat。

爲動畫使用CABasicAnimation ......如果你不知道如何設置那些一上來有豐富的資源在網絡上。