转场动画CATransition应用
2017-08-14 本文已影响52人
被吹落的风
CATransition类实现层的转场动画。你可以从一组预定义的转换或者通过提供定制的CIFilter实例来指定转场效果。
重点说明以下两个属性:
type:转场动画的类型,默认类型为kCATransitionFade。
- kCATransitionFade 交叉淡化过渡(不支持过渡方向)
- kCATransitionMoveIn 新视图把旧视图推出去
- kCATransitionPush 新视图移到旧视图上面
- kCATransitionReveal 将旧视图移开,显示下面的新视图
- kCATransitionCube 立方体翻滚效果
- kCATransitionOglFlip 上下左右翻转效果
- kCATransitionSuckEffect 收缩效果,如一块布被抽走(不支持过渡方向)
- kCATransitionRippleEffect 滴水效果(不支持过渡方向)
- kCATransitionPageCurl 向上翻页效果
- kCATransitionPageUnCurl 向下翻页效果
- kCATransitionCameraIrisHollowOpen 相机镜头打开效果(不支持过渡方向)
- kCATransitionCameraIrisHollowClose 相机镜头关上效果(不支持过渡方向)
- kCATransitionReveal 底部显出来
subtype:转场动画将要去往的方向。
- kCATransitionFromRight;
- kCATransitionFromLeft(默认值)
- kCATransitionFromTop;
- kCATransitionFromBottom
代码示例:
- 首先,初始化两个视图,一个红色,一个黄色。
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"改变1" forState:UIControlStateNormal];
button.frame = CGRectMake(10, 10, 300, 40);
[button addTarget:self action:@selector(changeUIView1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:@"改变2" forState:UIControlStateNormal];
button2.frame = CGRectMake(10, 120, 300, 40);
[button2 addTarget:self action:@selector(changeUIView2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
下面采用了两种动画方式,一种UIViewAnimation的形式,一种CATransition的形式。
- (void)changeUIView1
{
[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:1.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
[UIView commitAnimations];
}
- (void)changeUIView2
{
CATransition *transition = [CATransition animation];
transition.delegate = self;
transition.duration = 1.5f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.type = @"moveIn";//另一种设置动画效果方法
transition.subtype = kCATransitionFromLeft;
[self.view exchangeSubviewAtIndex:3 withSubviewAtIndex:2];
[self.view.layer addAnimation:transition forKey:@"animation"];
}