iOS动画—— Core Animation

一. Core Animation类简介

  • Core Animation,译为核心动画,是苹果提供的非常强大的动画处理API
  • Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程。
  • Core Animation是作用于CALayer上的动画,每一个继承与UIView的子类都有一个layer属性,可以通过这个layer来为更改相应view的形式,或者做动画。
  • Core Animation框架是基于OpenGL与CoreGraphics图像处理框架的一个跨平台的动画框架。将图像读取成位图,通过硬件的处理,实现动画效果
1. CoreAnimation与UIKit框架的关系
2. Core Animation类的继承关系图

在CoreAnimation中,大部分的动画效果都是通过Layer层来实现的,通过CALayer,我们可以组织复杂的层级结构。Layer层并不决定视图的展现,它只是存储了视图的几何属性状态

3. 锚点对几何属性的影响
@property CGPoint anchorPoint;  
@property CGPoint position;  
  • position:它是用来设置当前的layer在父控件当中的位置的.
    所以它的坐标原点.以父控件的左上角为(0.0)点.
  • anchorPoint: "锚点",取值范围都是0~1,默认值为(0.5, 0.5)。决定着CALayer身上的哪个点会在position属性所指的位置

position与anchorPoint关系


position和anchorPoint(1).png
position和anchorPoint(2).png

二. CABasicAnimation

CAAnimation
//动画速度
@property(nullable, strong) CAMediaTimingFunction *timingFunction;
kCAMediaTimingFunctionLinear  线性,匀速
kCAMediaTimingFunctionEaseIn  渐进,逐渐进入,然后加速完成
kCAMediaTimingFunctionEaseOut 渐出,动画全速进入,然后缓慢完成
kCAMediaTimingFunctionEaseInEaseOut  渐进渐出

//默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。
//如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode
@property(getter=isRemovedOnCompletion) BOOL 
CAMediaTiming

CAMediaTimingCAAnimation遵守的协议

beginTime   动画的开始时间(如果超过的duration,那么动画不会开始)
duration    动画的持续时间
speed       动画的速度
repeatCount 重复次数,默认为0,无限重复可以设置为HUGE_VALF或者MAXFLOAT
repeatDuration 动画重复时间,默认为0
fillMode     决定当前对象在非active时间段的行为。(要想fillMode有效,最好设置removedOnCompletion = NO)

fillMode类型:
    kCAFillModeRemoved     这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
    kCAFillModeForwards    当动画结束后,layer会一直保持着动画最后的状态
    kCAFillModeBackwards   在动画开始前,只需要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始。
    kCAFillModeBoth        这个其实就是上面两个的合成,动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态
CAAnimationDelegate 代理
// 动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim;
// 动画停止的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
CABasicAnimation

1.创建动画
2.设置相关属性
3.将动画添加到某个layer

// 创建动画  Layer的属性作为关键路径进行注册。
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
// 添加动画
- (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key;
// 移除动画
- (void)removeAnimationForKey:(NSString *)key;
- (void)removeAllAnimations;

keyPath 为CALayer的属性值,并对它的值进行修改,以达到对应的动画效果,需要注意的是部分属性值是不支持动画效果的。

transform.scale (比例转换)、
transform.scale.x
transform.scale.y
transform.rotation.z

 transform.rotation   (旋转) 
transform.rotation.x  (绕x轴旋转)
transform.rotation.y  (绕y轴旋转)
transform.rotation.z  (绕z轴旋转)

opacity  (透明度)
margin  布局
backgroundColor  (背景色)
cornerRadius   (圆角)
borderWidth  (边框宽)
bounds   (大小)
frame  (大小位置)
hidden (显示隐藏)

contents    (内容)
contentsRect  (内容大小)
cornerRadius (大小)
mask
masksToBounds

shadowColor(阴影色)、
shadowOffset
shadowOpacity
shadowOpacity

动画的起始与结束位置

//  指定属性(keyPath)开始的值
@property(nullable, strong) id fromValue;
// 指定属性结束时的值
@property(nullable, strong) id toValue;
// 在原有的值上改变
@property(nullable, strong) id byValue;
  • fromValuetoValue不为空:动画的值由fromValue变化到toValue

  • fromValuebyValue不为空:动画的值由fromValue变化到fromValue+byValue

  • byValuetoValue不为空:动画的值由toValue-byValue变化到toValue

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

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

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

CABasicAnimation可以设定keyPath的起点,终点的值,动画会沿着设定点进行移动,CABasicAnimation可以看成是只有两个关键点的特殊的CAKeyFrameAnimation。

演示
- (void)position {
    CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"position"];
    ani.toValue = [NSValue valueWithCGPoint:self.centerShow.center];
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.cartCenter.layer addAnimation:ani forKey:@"PostionAni"];
}

CAAnimationGroup 组动画

CAAnimationGroup可以保存一组动画对象(比如可以把前面的2中动画结合起来),将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行。也可以让动画按照时间依次串行

/// 依次串行
-(void)animationGroup3{
    CFTimeInterval currentTime = CACurrentMediaTime();
    //位移动画
    CABasicAnimation *anima1 = [CABasicAnimation animationWithKeyPath:@"position"];
    anima1.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
    anima1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-75)];
    anima1.beginTime = currentTime;
    anima1.duration = 1.0f;
    anima1.fillMode = kCAFillModeForwards;
    anima1.removedOnCompletion = NO;
    [_animationLayer addAnimation:anima1 forKey:@"aa"];
    //缩放动画
    CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    anima2.fromValue = [NSNumber numberWithFloat:0.8f];
    anima2.toValue = [NSNumber numberWithFloat:2.0f];
    anima2.beginTime = currentTime+1.0f;
    anima2.duration = 1.0f;
    anima2.fillMode = kCAFillModeForwards;
    anima2.removedOnCompletion = NO;
    [_animationLayer addAnimation:anima2 forKey:@"bb"];
    //旋转动画
    CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
    anima3.beginTime = currentTime+2.0f;
    anima3.duration = 1.0f;
    anima3.fillMode = kCAFillModeForwards;
    anima3.removedOnCompletion = NO;
    [_animationLayer addAnimation:anima3 forKey:@"cc"];
}
截屏2022-04-29 15.57.37.png

CAKeyframeAnimation 关键帧动画

CABasicAnimation 不同的是CAKeyframeAnimation会使用一个NSArray保存一组关键帧,而且通过绘图来指定路径。而CABasicAnimation只能从一个数值(fromValue)变换成另一个数值(toValue

-(void)keyFrameAnimation{
   CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
   NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
   NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
   NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
   NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
   NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
   
   anima.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
   anima.duration = 2.0f;
   anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];//设置动画的节奏
   anima.delegate = self;//设置代理,可以检测动画的开始和结束
   [_demoView.layer addAnimation:anima forKey:@"keyFrameAnimation"];
}

Demo

GitHub: https://github.com/iOSlixiang/Animations.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容