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

 代码二:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值