core Animation之CATransition(转场动画)

简介:

用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。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

 代码二:


相关文章
|
图形学
Core Animation -关键帧动画
Core Animation -关键帧动画
82 0
Core Animation - 渐变色CAGradientLayer
Core Animation - 渐变色CAGradientLayer
73 0
Core Animation - 图层行为
Core Animation - 图层行为
58 0
Core Animation - 图层行为
|
iOS开发 MacOS
Core Animation - 图层几何学<二>
Core Animation - 图层几何学<二>
66 0
|
iOS开发
Core Animation - 图层几何学<一>
Core Animation - 图层几何学<一>
74 0
Core Animation - 图层几何学<一>
Core Animation - 视觉效果<二>
Core Animation - 视觉效果<二>
32 0
|
iOS开发
Core Animation - 视觉效果<一>
Core Animation - 视觉效果<一>
74 0
|
算法 iOS开发
Core Animation - 视觉效果<三>
Core Animation - 视觉效果<三>
49 0
Core Animation - CATransformLayer的运用
Core Animation - CATransformLayer的运用
47 0
|
图形学
Core Animation - CATextLayer和富文本
Core Animation - CATextLayer和富文本
77 0