CABaseAnimation

CABaseAnimation是图层的属性值的简单动画,也是基于layer层来做的,,直接看代码看效果。

新建一个view作为测试对象 

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];

基于bounds属性的动画改变layer的bounds。

CABasicAnimation *animation = [CABasicAnimation animation];
    animation.duration = 3.0;
    animation.fromValue = @[@(100),@(100),@(20),@(20)];
    animation.toValue = @[@(100),@(100),@(200),@(200)];
    animation.repeatCount = MAXFLOAT;
    animation.autoreverses = YES;
    [view.layer addAnimation:animation forKey:@"bounds"];

基于backgroundColor属性的显示动画

 CABasicAnimation *animation2 = [CABasicAnimation animation];
    animation2.duration = 3.;
    animation2.fromValue = @[(id)[UIColor redColor].CGColor];
    animation2.toValue = @[(id)[UIColor yellowColor].CGColor];
    animation2.repeatCount = MAXFLOAT;
    animation2.autoreverses = YES;
    [view.layer addAnimation:animation2 forKey:@"backgroundColor"];

基于position的显示动画

 CABasicAnimation *animation3 = [CABasicAnimation animation];
    animation3.duration = 3.;
    animation3.fromValue = @[@(150)];
    animation3.toValue = @[@(200)];
    animation3.repeatCount = MAXFLOAT;
    animation3.autoreverses = YES;
    [view.layer addAnimation:animation3 forKey:@"position.x"]
......由于是基于简单属性值进行的动画,所以我们很有必要去看下API的那些属性是animation的,到CALayer类里面可以看到

bounds、position、anchorPointZ等和他们的构成结构体浮点型属性可以动画的,还有hidden、masksToBounds等BOOL型的也可以动画,更有mask、transform等都是可以动画的。

简单介绍下以上动画的各个属性的作用

 CABasicAnimation *animation3 = [CABasicAnimation animation];  //创建动画实例,可以指定path也就是属性也可以在最后addAnimation时候指定
 animation3.duration = 3.;   //动画时间
 animation3.fromValue = @[@(150)];  //指定的动画开始值,不能为nil  id类型
 animation3.toValue = @[@(200)];  //指定的动画结束值,不能为nil  id类型
 animation3.repeatCount = MAXFLOAT;   //动画重复次数
 animation3.autoreverses = YES;  //结束之后是否复原到最开始状态
 [view.layer addAnimation:animation3 forKey:@"position.x"];  //添加动画到图层,可以指定属性作为path



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值