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