iOS的CoreAnimation开发框架中的Layer层动画制作解析

2020-01-15 16:34:26于海丽

//如果设置为yes,则为此执行都会在上一次执行的基础上执行
@property(getter=isCumulative) BOOL cumulative;
//这个属性和transfron属性的动画执行相关
@property(nullable, strong) CAValueFunction *valueFunction;
上面这些属性中,只有一个需要我们注意,valueFunction是专门为了transform动画而设置的,因为我们没有办法直接改变transform3D中的属性,通过这个参数,可以帮助我们直接操作transfrom3D属性变化产生动画效果,举例如下,一个绕Z轴旋转的动画:

 //绕z轴旋转的动画
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    //从0度开始
    ani.fromValue = @0;
    //旋转到180度
    ani.toValue = [NSNumber numberWithFloat:M_PI];
    //时间2S
    ani.duration = 2;
    //设置为z轴旋转
    ani.valueFunction = [CAValueFunction functionWithName:kCAValueFunctionRotateZ];
    //执行动画
    [layer addAnimation:ani forKey:@""];

实际上,使用点的方式也是可以访问到相应属性的,如果不设置valueFunction,使用如下方法也是可以进行绕Z轴旋转的:

//绕z轴旋转的动画
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //从0度开始
    ani.fromValue = @0;
    //旋转到180度
    ani.toValue = [NSNumber numberWithFloat:M_PI];
    //时间2S
    ani.duration = 2;
    //执行动画
    [layer addAnimation:ani forKey:@""];

3.CABasicAnimation属性

CABasicAnimaton是CAPropertyAnimation分出来的一个子类,创建基础的属性变化动画,例如我们上面的示例代码,其中属性如下:

@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;

上面三个属性都是来确定动画的起始与结束位置,有如下的含义:

(1)fromValue和toValue不为空:动画的值由fromValue变化到toValue

(2)fromValue和byValue不为空:动画的值由fromValue变化到fromValue+byValue

(3)byValue和toValue不为空:动画的值由toValue-byValue变化到toValue

(4)只有fromValue不为空:动画的值由fromValue变化到layer的当前状态值

(5)只有toValue不为空:动画的值由layer当前的值变化到toValue

(6)只有byValue不为空:动画的值由layer当前的值变化到layer当前的值+byValue