使用CAAnimation实现CATransition推送

时间:2011-04-09 11:25:34

标签: iphone ios core-animation caanimation catransition

如何在iOS中使用kCATransitionPush子类实现CAAnimation

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,这将有助于我更好地理解核心动画从头开始过渡。

2 个答案:

答案 0 :(得分: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;
}

使用布尔前进,您可以模拟“从左”或“从右”的过渡。

答案 1 :(得分:4)

默认kCATransitionPush确实包含淡入淡出。要使用您自己的CABasicAnimation复制转换,您首先需要了解推送动画的工作原理。我这样做没有测试所以这可能会关闭,但如果我没记错,子层B替换子层A的动画就像这样:

  • 图层A从原始位置移动到右侧
  • 图层B从A
  • 的右侧位置移动到原始位置
  • 动画图层B的第一半动画显示其不透明度从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];