用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点
UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果
属性解析:
type:动画过渡类型
subtype:动画过渡方向
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)
代码一:
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView *imgView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
[self.view addSubview:self.imgView];
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"上一张" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn1.tag=1;
btn1.backgroundColor=[UIColor redColor];
btn1.frame=CGRectMake(50, 100, 80,40);
[self.view addSubview:btn1];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setTitle:@"下一张" forState:UIControlStateNormal];
btn2.tag=2;
[btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn2.frame=CGRectMake(150, 100, 80,40);
btn2.backgroundColor=[UIColor redColor];
[self.view addSubview:btn2];
}
-(void)btnClick:(id)sender
{
UIButton *btn=(UIButton *)sender;
if (btn.tag==1) {
self.imgView.image=[UIImage imageNamed:@"1.jpg"];
CATransition *transition=[CATransition animation];
//动画效果
transition.type=@"push";
//动画方向
transition.subtype=kCATransitionFromRight;
transition.duration=2.0;
[self.imgView.layer addAnimation:transition forKey:nil];
}
else
{
self.imgView.image=[UIImage imageNamed:@"2.jpg"];
CATransition *transition=[CATransition animation];
transition.type=kCATransitionMoveIn;
transition.subtype=kCATransitionFromBottom;
transition.duration=2.0;
[self.imgView.layer addAnimation:transition forKey:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
代码二: