iOS之动画学习笔记二

来源:互联网 发布:php 数据库连接池 编辑:程序博客网 时间:2024/05/18 01:35

       今天,我趁着项目空暇之余,把有关CAAnimation以及它的子类的相关属性和方法都罗列一遍。以便将来在忘记的时候能够快速拾起。

一、CAAnimation(The base animation class)

它有两个私有属性:

void *_attr;

uint32_t _flags;

// 暂时不知道它的用途 -。- 以后补上。


+ (instancetype)animation;

// 创建动画实例对象的工厂方法


+ (nullable id)defaultValueForKey:(NSString *)key; 

// 苹果文档上这么描述:

Specifies the default value associated with the specified key.The default value for the named property. Returns nil if no default value has been set.If key is not a known for property of the class, the result of the method is undefined.

       也就是说我们根据属性名可以得到一个相关的值,假如我们没有设置那么就会返回nil。相当于一种全局设置。一般用在自定义的动画中,与CALayer的同名方法一样。

       例如我们在自定义的动画的实现文件中覆写父类的这个方法。那么,我们在用到这个自定义动画的时候,在不设置时间duration的情况下,都是2s。

+ (id)defaultValueForKey:(NSString *)key {

    if ([keyisEqualToString:@"duration"]) {

        return 2.f;

    }

    return [superdefaultValueForKey:key];

}


- (BOOL)shouldArchiveValueForKey:(NSString *)key;

// 子类重写这个方法设置属性是否可以被归档


@property(nullable,strong)CAMediaTimingFunction *timingFunction;

// 一个动画与时间的函数。默认的函数是一条直线,也就是动画均匀变化

// 例如:[anime setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];


@property(nullable,strong)id delegate;

// 动画代理,一共有两个代理方法:

- (void)animationDidStart:(CAAnimation *)anim;

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;


@property(getter=isRemovedOnCompletion)BOOL removedOnCompletion;

// 当这个值是YES的时候,当前动画对象会在动画时间duration结束的时候会从render tree树上移除掉。默认是YES

// 这个属性可以配合fillMode使用;以及代理方法DidStop...例如在用代理方法DidStop设置一个动画结束后执行另一个方法,那么我们必须确保前一个动画存在。因此 anim.removedOnCompletion = YES;


二、CAPropertyAnimation(继承自CAAnimation)

+ (instancetype)animationWithKeyPath:(nullableNSString *)path;

// 创建动画实例的同时定义动画的方式


@property(nullable,copy)NSString *keyPath;

// 用来描述动画的方式

// 例如:anim.keyPath = @"position";  // 平移

anim.keyPath = @"lineWidth"; // 线宽

anim.keyPath = @"strokeEnd"; // 画线结束动画

...


@property(getter=isAdditive)BOOL additive;

@property(getter=isCumulative)BOOL cumulative;

@property(nullable,strong)CAValueFunction *valueFunction;

// 暂时摸不清楚它们的用途,不过我们可以试着理解文档的意思

// 在这之前先了解一下layer。我在一篇博客中看到,有句话这么说的,一个个Layer通过tree的结构组织起来,在Display的过程中实际上有3种Layer tree。

  • model layer tree
  • presentation tree
  • render tree
  • model Layer tree 中的Layer是我们通常意义说的Layer。当我们修改layer中的属性时,就会立刻修改model layer tree。
  • presentation tree 是Layer在屏幕中的真实位置。比如我们创建一个动画
  • render tree 在apple的render server进程中,是真正处理动画的地方。而且线程的优先级也比我们主线程优先级高。所以有时候即使我们的App主线程busy,依然不会影响到手机屏幕的绘制工作
// valueFunction 在设置这个属性后,将会被插入到当前的动画目标属性中。不过具体怎样的功能暂时没找到,有知道大神劳烦告诉一下。

三、CABasicAnimation(继承自CAPropertyAnimation)
这个比较长用,用法也比较简单

@property(nullable,strong)id fromValue;

@property(nullable,strong)id toValue;

@property(nullable,strong)id byValue;

可以看我第一篇的有关CAShapLayer的动画,其中就有例子介绍,这里就不过多阐述。

四、CAKeyAnimation(继承自CAPropertyAnimation)

@property(nullable,copy)NSArray *values;

// 每一帧动画对应的值,这个值可以参考CABasicAnimation里面的三个value属性。


@property(nullable)CGPathRef path;

// 当我们设置path的值的时候,会重写values数组中的值

// 和BezierPath一起结合使用

// 例如:

anim.keyPath =@"position";

anim.path = [[UIBezierPathbezierPathWithArcCenter:CGPointMake(200,200)radius:100startAngle:0endAngle:M_PI *2 clockwise:YES]CGPath];

我们可以让制定layer按bezierPath设定的那样运动。


@property(nullable,copy)NSArray<NSNumber *> *keyTimes;

// 和values相对的,表示每一帧动画的时刻,这个要看总的key Animation 的时长。它的取值范围是[0,1];

// 例如:keyAnim.keyTimes =@[@0.2f,@0.5f, @0.75f, @1.0f];也就是在keyAnim.duration的百分之20执行values的一个动画,百分之50的时候执行values的下一个,一次类推。


@property(nullable,copy)NSArray<CAMediaTimingFunction *> *timingFunctions;

// 这个是动画显示的方式,如渐进,渐出等

// 这里要注意,当我们在values中设置有n个值的时候,那么在timingFunctions中就改设置n-1个值。每一个function描述的都是从一帧到下一帧的过程。


@property(copy)NSString *calculationMode;

// calculation计算

// 人为指定关键帧动画的持续方式(或显示方式),一旦设定这个值后,keyTimes 和 timingFunctions都将失去作用。

// 它有5种形式的显示:

kCAAnimationLinear // 直线型的,默认

kCAAnimationDiscrete // 离散型的,就是短短续续的感觉

kCAAnimationPaced // 匀速的

kCAAnimationCubic // 字面意思是立方体的,想过上就感觉先快后慢,和弹簧那样很自然

kCAAnimationCubicPaced // 结合体,感觉效果很机械


@property(nullable,copy)NSArray<NSNumber *> *tensionValues;

@property(nullable,copy)NSArray<NSNumber *> *continuityValues;

@property(nullable,copy)NSArray<NSNumber *> *biasValues;

// 这三个属性是对每一帧的细节上的设置,分别是张力值、持续性值、偏移值。具体的我也没去测试,如果有知道的劳烦告知下,哈


@property(nullable,copy)NSString *rotationMode;

// 确定动画对象是否允许按路径移动的时候去匹配路径的切面。意思就是在移动的时候要不要使layer也旋转来匹配path的切面,与path属性结合使用。在没有设置path的时候,这个属性不起作用。默认nil

// 一共两个option:

kCAAnimationRotateAuto // 头朝外(我也解释不清楚,自己去试试就明白了)

kCAAnimationRotateAutoReverse // 头朝内


五、CASpringAnimation(继承自CABasicAnimation)

// 过场动画的用法就是在创建好一个视图比如ImageView的时候,给这个视图添加一个CATransition,然后在后面加上不同的图片,效果就出来了。

@property CGFloat mass;

// 在弹簧动画结束时的质量,至少要大于0,默认是1.


@property CGFloat stiffness;

// 弹簧钢度系数,必须大于0,默认100.


@property CGFloat damping;

// 弹簧阻尼系数,必须大于等于0,默认10.


@property CGFloat initialVelocity;

// 初速度,默认0

@property(readonly)CFTimeInterval settlingDuration;

六、CATransition(继承自CAAnimation)

@property(copy)NSString *type;

// 转场动画的类型

// 附上一张从别处拿来的type图。不过我发现下面有好几种方法都没找到-。~

这里写图片描述


@property(nullable,copy)NSString *subtype;

// 子类型的过场形势

// 四种:

kCATransitionFromRight

kCATransitionFromLeft

kCATransitionFromTop

kCATransitionFromBottom


@propertyfloat startProgress;

@property float endProgress;

// 范围0~1


@property(nullable,strong)id filter;

// 暂不明白作用

    今天就写着么多了,有好多都不明白-。~ ,没办法,谁让我是小白呢。以后我会不定时的写自己学习的笔记,希望我们能够一起进步。




1 0
原创粉丝点击