一.了解CoreAnimation

中心动画的内部完成架构, 是根据OpenGL/OpenGL ES, Core Graphics/ Metal 基础上封装的, OpenGL/OpenGL ES, Core Graphics/ Metal运用成本都很高,苹果未来更好的让开发者做出好的动画效果,封装出CoreAnimation

核心动画-图层结构讲解
核心动画-图层结构讲解

二 Core Animation Introduction

  • 简略易易⽤用的⾼高功能混合编程模型\
  • ⽤用类似于视图⼀相同,使⽤用图层来创立复杂的编程接⼝口
  • 轻量量化的数据结构,它能够同时显现让上百个图层产⽣生动画效果
  • 一套⾮十分较简略的动画接⼝口,能让动画运⾏行行在独⽴立的线程中,并能够独⽴立于主线程之外.
  • 一旦动画装备完成并启动,核⼼心动画就能独⽴立并彻底操控相应的动画帧.
  • 提⾼高应⽤用功能.应⽤用程序只有当发⽣生改变的时候才会重绘内容.使⽤用Core Animation能够不不使⽤用其他图形API,例例如OpenGL来获取⾼高效的动画功能.
  • 灵敏的布局管理理模型,答应图层相对同级图层的联系来设置特点的方位和⼤大⼩小

三 Core Animation分类

核心动画-图层结构讲解

what’s the UIView ? what’s the CALayer ? 为什么要将UIView与CALayer提供2个平级层级联

答:

核心动画-图层结构讲解

为何开发者需要运用CALayer

答:

核心动画-图层结构讲解

四: Core Animation Class

核心动画-图层结构讲解

图层几何

核心动画-图层结构讲解
旋转时: frame的宽高是会改变的

核心动画-图层结构讲解

核心动画-图层结构讲解

五: CALayer常用特点

  • Hit Testing Hit Test底层完成思路
  • 常见的视图不相应事件情况

核心动画-图层结构讲解

核心动画-图层结构讲解

核心动画-图层结构讲解

核心动画-图层结构讲解

运用场景

核心动画-图层结构讲解

  1. 事件穿透。 自己不影响,让父视图响应 本类中完成

    核心动画-图层结构讲解

  2. 子视图超过父视图(子视图响应)

本类中完成

核心动画-图层结构讲解

  • contents 设置view背景
self.view.layer.contents = (__bridge id)image.CGImage;
self.view.contentMode  // 能够设置图片显现形式
  • contentsScale
self.view.layer.contentsScale = [UIScreen mainScreen].scale
/// 会影响图片显现的清晰度
  • zPosition:z轴坐标 涉及到OpenGL SL的知识,请移步,说简略点便是面向屏幕的坐标,平时默许是0, 能够通过这个特点,让图层渲染展示在前还是后
    核心动画-图层结构讲解

六 : 动画实例

隐式动画: blog.csdn.net/u013248706/… 设置非相关layer的背景颜色,apha等,回发生隐式动画 ,默许0.25s UIVview的layer是相关的,默许隐式动画封闭 非相关便是单独创立的layer

//动画2
  //begin a new transaction
  [CATransaction begin];
  //set the animation duration to 2 second
  [CATransaction setAnimationDuration:2.0];
  _layer.backgroundColor = [UIColor orangeColor].CGColor;
    [CATransaction commit];

显现动画

//动画1
  CABasicAnimation *animation = [CABasicAnimation animation];
  animation.keyPath = @"position.y";
  animation.toValue = @600;
  animation.duration = 1;
  //处理动画康复到初始方位
  animation.removedOnCompletion = **NO**;
  animation.fillMode = kCAFillModeForwards;
  [_redView.layer addAnimation:animation forKey:**nil**];

核心动画-图层结构讲解

CALayer宗族

核心动画-图层结构讲解

代码

#import "ViewController.h"
/*
动画增加步骤:
1.找演员CALayer,确定动画主角
2.写剧本CAAnimation,规则动画怎么样改换
3.开拍AddAnimation,开始履行
*/
**@interface** ViewController ()
**@property** (**weak**, **nonatomic**) **IBOutlet** UIView *redView;
**@property** (**nonatomic**,**strong**) CALayer *layer;
**@property** (**nonatomic**,**strong**) UIView *layer3;
\
**@end**
\
**@implementation** ViewController
\
- (**void**)viewDidLoad {
  [**super** viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
 
  CALayer *layer = [CALayer layer];
  layer.frame = CGRectMake(100, 100, 100, 100);
  layer.backgroundColor = [UIColor greenColor].CGColor;
  _layer = layer;
  [**self**.view.layer addSublayer:layer];
\
  _layer3 = [UIView new];
  _layer3.frame = CGRectMake(150, 100, 100, 100);
  _layer3.backgroundColor = [UIColor yellowColor];
//  _layer3.layer.zPosition = -2.0;
  [**self**.view addSubview:_layer3];
}
\
- (**void**)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
  _layer3.layer.zPosition = -2.0;
  //动画1
  CABasicAnimation *animation = [CABasicAnimation animation];
  animation.keyPath = @"position.y";
  animation.toValue = @600;
  animation.duration = 1;
\
  //处理动画康复到初始方位
  animation.removedOnCompletion = **NO**;
  animation.fillMode = kCAFillModeForwards;
  [_redView.layer addAnimation:animation forKey:**nil**];
  //动画2
  //begin a new transaction
  [CATransaction begin];
 
  //set the animation duration to 1 second
  [CATransaction setAnimationDuration:2.0];
 
  _layer.backgroundColor = [UIColor orangeColor].CGColor;
   
  [CATransaction setCompletionBlock:^{
    //rotate the layer 90 degrees
    CGAffineTransform transform = **self**.layer.affineTransform;
    transform = CGAffineTransformRotate(transform, M_PI_2 * 0.5);
    **self**.layer.affineTransform = transform;
  }];
//  //commit the transaction
  [CATransaction commit];
}