2011-04-09 31 views
6

如何在iOS中使用CAAnimation子類實現kCATransitionPush使用CAAnimation實現CATransition推送

CAAnimation *animation; 
// How do you create an animation that does the same than: 
// CATransition *animation = [CATransition animation]; 
// [animation setType:kCATransitionPush];   
[self.view.layer addAnimation:animation forKey:nil]; 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1];   
[self.view addSubview:change]; 
[UIView commitAnimations]; 

我知道,UIView動畫可以also使用,但它會幫助我瞭解核心動畫更好,如果我可以實現從地面向上一kCATransitionPush過渡。

回答

11

爲了時間在兩層同時執行的動畫,你必須足夠CAAnimationGroup添加到每個層。

[nextView.layer addAnimation:nextViewAnimation forKey:nil]; 
[currentView.layer addAnimation:currentViewAnimation forKey:nil]; 

nextViewAnimation是:

CAAnimationGroup *nextViewAnimation = [CAAnimationGroup animation]; 
NSMutableArray *nextAnimations = [NSMutableArray array]; 

[nextAnimations addObject:[self opacityAnimation:YES]]; // fade in 

CGPoint fromPoint = CGPointMake(forward ? nextView.center.x + nextView.frame.size.width : nextView.center.x - nextView.frame.size.width, nextView.center.y); 
[nextAnimations addObject:[self positionAnimationFromPoint:fromPoint toPoint:nextView.center]]; // traslation in 

nextViewAnimation.animations = nextAnimations; 

和currentViewAnimation:

CAAnimationGroup *currentViewAnimation = [CAAnimationGroup animation]; 
NSMutableArray *currentAnimations = [NSMutableArray array]; 
[currentSceneAnimations addObject:[self opacityAnimation:NO]]; // fade out 

CGPoint toPoint = CGPointMake(forward ? currentView.center.x - currentView.frame.size.width : currentView.center.x + currentView.frame.size.width, currentView.center.y); 
    [currentAnimations addObject:[self positionAnimationFromPoint:currentView.center toPoint:toPoint]]; // traslation out 

currentViewAnimation.animations = currentAnimations; 

這些方法創建基本的動畫:

- (CABasicAnimation *)opacityAnimation:(BOOL)fadeIn { 
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
a.fromValue = [NSNumber numberWithFloat:fadeIn ? 0.0 : 1.0]; 
a.toValue = [NSNumber numberWithFloat:fadeIn ? 1.0 : 0.0]; 
return a; 
} 

- (CABasicAnimation *)positionAnimationFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint { 
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"position"]; 
a.fromValue = [NSValue valueWithCGPoint:fromPoint]; 
a.toValue = [NSValue valueWithCGPoint:toPoint]; 
return a; 
} 

用布爾轉發您可以模擬「從左邊」或「從右邊」的過渡。

4

默認kCATransitionPush確實包含淡入淡出。要使用自己的CABasicAnimation複製轉場,您首先需要了解推動動畫的工作原理。我這樣做是未經測試所以這可能是關閉的,但如果我沒有記錯,其中子乙替換子層動畫是這樣的:

  • 從原來的位置A層移動到右側
  • 層B從移動右到動畫層B的
  • Lirst一半的原始位置動畫其不透明度爲0〜1
  • 動畫層A的第二半動畫化從1其不透明度爲0 (當然也可以替換權利這裏有任何方向)。

CABasicAnimation只支持動畫一個屬性,所以你將不得不創建一個CAAnimationGroup控制4個不同的動畫。

創建位置和不透明度動畫這樣的:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; 
anim.fromValue = [NSValue valueWithCGPoint:startPoint]; 
anim.toValue = [NSValue valueWithCGPoint:endPoint]; 

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
anim.fromValue = [NSNumber numberWithFloat:0.0]; 
anim.toValue = [NSNumber numberWithFloat:1.0]; 
+0

謝謝Joris。這不是視圖A替換視圖B.這是視圖的原始狀態被新狀態取代。 – hpique 2011-04-13 07:04:47

+0

對不起,不知何故,我使用單詞視圖,我應該使用CALayer。一個轉換確實對View的內容有效:我不知道Apple的實現,可能是支持CALayer內容動畫的OpenGL。最好的方法來自己做,將他們上面描述的方式應用於子層。 – 2011-04-13 07:51:27

+0

你如何區分子層A和子層B? CATransition似乎自動執行此操作。 – hpique 2011-10-17 18:34:20