如何在kCATransitionPush中使用CAAnimation子类实现iOS?
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转换,那么它将帮助我更好地理解核心动画。
发布于 2011-10-24 15:27:31
为了在这两个层上同时执行动画,您必须向每个层添加足够的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;
}使用布尔型正向,您可以模拟“从左”到“从右”的转换。
发布于 2011-04-12 22:52:16
默认的kCATransitionPush确实包括淡出。要使用自己的CABasicAnimation复制转换,首先需要了解push动画是如何工作的。我在没有测试的情况下这样做,所以这可能是关闭的,但是如果我正确地记住了B子层替换子层A的动画,则如下所示:
F 211
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];https://stackoverflow.com/questions/5604718
复制相似问题