2012-07-22 51 views
0

我有兩個CALayer對象 - layer1,layer2。我想執行以下操作 -如何同步添加一個新的CALayer對象與另一個對象的動畫

  • 加層1到視圖的層用戶addSublayer:
  • 舉動層1到一個新的位置使用CABasicAnimation
  • 添加二層到視圖的層
  • 移動二層到一個新的位置

這,沒有layer2顯示,直到layer1的動畫完成。

我試圖通過將layer2的動畫的beginTime設置爲layer1的動畫的結束時間 - 但layer2在開始時間之前在間隙中顯示。

有什麼建議嗎?

回答

0

我發現了一種方法來實現我的結果。我不確定這是做這件事的最好方法,所以仍然會對其他想法感激。

我同時創建layer1和layer2,addSublayer都創建到view.layer。然後我動畫層1將其移動到一個新的位置,持續時間= 0.3。對於圖層2,我使用由移動(beginTime = 0.3,持續時間= 0.3)和另一個使用隱藏屬性的動畫組成的組進行動畫處理。後面的動畫會立即隱藏layer2,然後在beginTime = 0.3時取消隱藏它。爲了實現後面的動畫,我使用了CAKeyFrameAnimation,因爲這個動畫必須是離散的 - 要麼完全隱藏,要麼完全不隱藏。

這是代碼。我通過刪除關於我的實現的細節來簡化它,所以這不是我的應用程序中的實際代碼 - 如果有任何錯誤,請致歉。

CALayer layer1 = [CALayer layer]; 
CALayer layer2 = [CALayer layer]; 

/* 
* set attributes of the layers -- cut out of this example 
*/ 
layer1.position = toPosition1; // positions when animation is complete 
layer2.position = toPosition2; 

[myView.layer addSublayer:layer1]; 
[myView.layer addSublayer:layer2]; 

// animate layer 1 
CABasicAnimation* move1 = [CABasicAnimation animationWithKeyPath:@"position"]; 
move1.duration = 0.3; 
move1.fromValue = fromPosition2; 
[layer1 addAnimation:move1 forKey:nil]; 

// animate layer 2 with a group 
CABasicAnimation* move2 = [CABasicAnimation animationWithKeyPath:@"position"]; 
move2.duration = 0.3; 
move2.fromValue = fromPosition2; 
move2.beginTime = 0.3; 

CAKeyframeAnimation *show = [CAKeyframeAnimation animationWithKeyPath:@"hidden"]; 
show.values = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNumber numberWithBool:NO], nil]; 
// times are given as fractions of the duration time -- hidden for first 50% of 0.6 sec 
show.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil]; 
show.calculationMode = kCAAnimationDiscrete; 
show.duration = 0.6; 
show.beginTime = 0; 

CAAnimationGroup *grp = [CAAnimationGroup animation]; 
[grp setAnimations:[NSArray arrayWithObjects:move2, show, nil ]]; 
grp.duration = 0.6; 

[layer2 addAnimation:grp forKey:nil]; 

相關問題