`
yidongkaifa
  • 浏览: 4051852 次
文章分类
社区版块
存档分类
最新评论

Core Animation基础介绍

 
阅读更多

Core Animation可以翻译为核心动画,它为图形渲染和动画提供了基础。使用核心动画,你只需要设置一些参数比如起点和终点,剩下的帧核心动画为你自动完成。核心动画使用硬件加速,不用消耗cpu资源。其实平时咱们开发的iOS应用都在有意无意的使用了核心动画。动画不会替代View,而是和View一起提供更好的性能。Core Animation通过缓存view上的内容到bitmap,这样bitmap就可以直接在图形硬件上操作。从而提高了性能。

核心动画所在的位置:



1、关于层类

Layer Classes是core animation的基础。Layer Classes提供了一个抽象的概念,这个概念对于那些使用NSview和UIview的开发者来说是很熟悉的。基础层是由CAlayer类提供的,CAlayer是所有Core Animation层的父类。
同一个视图类的实例一样,一个CAlayer实例也有一个单独的superlayer和上面所有的子层(sublayers),它创建了一个有层次结构的层,我们称之为layer tree。layers的绘制就像views一样是从后向前绘制的,绘制的时候我们要指定其相对与他们的superlayer的集合形状,同时还需要创建一个局部的坐标系。layers可以做一些更复杂的操作,例如rotate(旋转),skew(倾斜),scale(放缩),和project the layer content(层的投影)。
图层的内容提供
(1)直接设置层的content属性到一个core graphics图,或者通过delegation来设置
(2)提供一个代理直接绘制到Core Graphics image context(核心图形的上下文)
(3)设置任意数量的所有层共有的可视的风格属性。例如:backgroundColor(背景色),opacity(透明度)和masking(遮罩)。max os x应用通过使用core image filters来达到这种可视化的属性。
(4)子类化CAlayer,同时在更多的封装方式中完成上面的任意技术。

1.1CALayer的子类和他们的使用场景

Class

Usage

CAEmitterLayer

Used to implement a Core Animation–based particle emitter system. The emitter layer object controls the generation of the particles and their origin.

CAGradientLayer

Used to draw a color gradient that fills the shape of the layer (within the bounds of any rounded corners).

CAEAGLLayer/CAOpenGLLayer

Used to set up the backing store and context needed to draw using OpenGL ES (iOS) or OpenGL (OSX).

CAReplicatorLayer

Used when you want to make copies of one or more sublayers automatically. The replicator makes the copies for you and uses the properties you specify to alter the appearance or attributes of the copies.

CAScrollLayer

Used to manage a large scrollable area composed of multiple sublayers.

CAShapeLayer

Used to draw a cubic Bezier spline. Shape layers are advantageous for drawing path-based shapes because they always result in a crisp path, as opposed to a path you draw into a layer’s backing store, which would not look as good when scaled. However, the crisp results do involve rendering the shape on the main thread and caching the results.

CATextLayer

Used to render a plain or attributed string of text.

CATiledLayer

Used to manage a large image that can be divided into smaller tiles and rendered individually with support for zooming in and out of the content.

CATransformLayer

Used to render a true 3D layer hierarchy, rather than the flattened layer hierarchy implemented by other layer classes.

QCCompositionLayer

Used to render a Quartz Composer composition. (OSX only)

1.2、anchorPoint、position

anchorPoint又称锚点,锚点对动画是有很大影响的。下图描述了基于锚点的三个示例值:



1.3、 图层的 frame、bounds、position 和 anchorPoint 关系如下图所示:


在该示例中,anchorPoint 默认值为(0.5,0.5),位于图层的中心点。图层的 position 值为(100.0,100.0),bounds 为(0.0,0.0,120,80.0)。通过计算得到图层的 frame为(40.0,60.0,120.0,80.0)。

如果你新创建一个图层,则只有设置图层的 frame 为(40.0,60.0,120.0,80.0),相应的 position 属性值将会自动设置为(100.0,100.0),而 bounds 会自动设置为 (0.0,0.0,120.0,80.0)。下图显示一个图层具有相同的 frame(如上图),但是在该图中它的 anchorPoint 属性值被设置为(0.0,0.0),位于图层的左下角位置。


图层的 frame 值同样为(40.0,60.0,120.0,80.0),bounds 的值不变,但是图层的 position 值已经改变为(40.0,60.0)。


2、关于动画类

核心动画的动画类使用基本的动画和关键帧动画把图层的内容和选取的属性动画的显示出来。所有核心动画的动画类都是从 CAAnimation 类继承而来。
CAAnimation 实现了 CAMediaTiming 协议,提供了动画的持续时间,速度,和重复计数。 CAAnimation 也实现了 CAAction 协议。该协议为图层触发一个动画动作提供了提供标准化响应。动画类同时定义了一个使用贝塞尔曲线来描述动画改变的时间函数。例如,一个 匀速时间函数(linear timing function)在动画的整个生命周期里面一直保持速度不变, 而渐缓时间函数(ease-out timing function)则在动画接近其生命周期的时候减慢速度。核心动画额外提供了一系列抽象的和细化的动画类,比如:CATransition 提供了一个图层变化的过渡效果,它能影响图层的整个内容。 动画进行的时候淡入淡出(fade)、推(push)、显露(reveal)图层的内容。这些过渡效 果可以扩展到你自己定制的 Core Image 滤镜。CAAnimationGroup 允许一系列动画效果组合在一起,并行显示动画。

2.1动画类

CAPropertyAnimation:是一个抽象的子类,它支持动画的显示图层的关键路 径中指定的属性一般不直接使用,而是使用它的子类,CABasicAnimation,CAKeyframeAnimation. 在它的子类里修改属性来运行动画。
CABasicAnimation: 简单的为图层的属性提供修改。 很多图层的属性修改默认会执行这个动画类。比如大小,透明度,颜色等属性
CAKeyframeAnimation: 支持关键帧动画,你可以指定的图层属性的关键路径动画,包括动画的每个阶段的价值,以及关键帧时间和计时功能的一系列值。在 动画运行是,每个值被特定的插入值替代。核心动画 和 Cocoa Animation 同时使用这些动画类。

2.2 如何使用多个动画效果叠加

在执行动画的过程中需要同时修改position,alpha, frame等属性,使用CAAnimationGroup可以将三个动画合成一起执行:

CAAnimationGroup *animGroup = [CAAnimationGroup animation]; 
animGroup.animations = [NSArray arrayWithObjects:moveAnim,scaleAnim,opacityAnim, nil];
animGroup.duration = 1;
[view.layer addAnimation:animGroup forKey:nil];

2.3事务管理类

图层的动画属性的每一个修改必然是事务的一个部分。CATransaction 是核心动画里面负责协调多个动画原子更新显示操作。事务支持嵌套使用。


2.4Core Animation类的继承关系图





分享到:
评论

相关推荐

    Core Animation基础介绍、简单使用CALayer以及多种动画效果

    Core Animation基础介绍、简单使用CALayer以及多种动画效果 详细讲解了iOS的动画的原理与应用非常值得深入学习

    Core Animation(二)动画基础部分

    上一篇简单的描述了Core Animation相关的内容,并且亲自动手实践了一个动画,同时也提到了“隐式动画”和“显示动画”,也提到了UIKit动画,可见iOS的动画部分确实有些内容需要掌握

    学习Core Animation例子CADemo1

    Core Animation包含于QuartzCore.framwork中,是iOS与OS X平台上负责图形渲染与动画的基础设施

    swift-ThinkVerb是一组基于CoreAnimation的API可以用非常少的代码快速生成基础动画

    ThinkVerb 是一组基于 CoreAnimation 的 API,可以用非常少的代码快速生成基础动画

    coreAnimation

    关于如何使用CAReplicatorLayer作倒影的动态更新的demon。涉及到一些关于core animation很基础很重要的概念。

    iOS动画(CoreAnimation)总结

    CoreAnimation是iOS和macOS平台上负责图形渲染与动画的基础框架。CoreAnimation可以作用与动画视图或者其他可视元素,为你完成了动画所需的大部分绘帧工作。你只需要配置少量的动画参数(如开始点的位置和结束点...

    使用CoreAnimation来模拟的iOS StackView .zip

    使用CoreAnimation来模拟的iOS StackView。 .zip,这个项目使用coreanimation来模拟ios中的stackview。我使用uiimageview作为stack的基础视图。

    Core Animation一些Demo总结 (动态切换图片、大转盘、图片折叠、进度条等动画效果)

    前一篇总结了Core Animation的一些基础知识,这一篇主要是Core Animation 的一些应用,涉及到CAShapeLayer、CAReplicatorLayer等图层的知识。 先看效果图: 1、切换图片: 2、彩票转盘 3、图片折叠 4、进度条...

    CoreAnimation:CoreAnimation系列

    #CoreAnimation CoreAnimation直译为核心动画,它包含了非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍。但它不仅仅只能做动画的,它是一个图形渲染和动画基础设施,可用于iOS和OS X,...

    非常好的IOS项目资源,分享出来.zip

    Core Animation是IOS和OS X平台上负责图形渲染与动画的基础框架。Core Animation可以作用与动画视图或者其他可视元素,为你完成了动画所需的大部分绘帧工作。你只需要配置少量的动画参数(如开始点的位置和结束点的...

    CoreAnimation

    ios核心动画,包括:CABasicAnimation基础动画、CAKeyframeAnimation帧动画、CATransition过度动画、CAAnimationGroup组动画。本代码主要对核心动画进行描述分析、并与UIView动画进行对比,注释都非常清晰,希望对...

    ThinkVerb:基于CoreAnimation的超酷动画界面

    ThinkVerb是一组基于CoreAnimation的API,可以直接使用CoreAnimation,ThinkVerb通过链式语法进行编程,并自管理CAAnimation,无需自己手动创建任何CAAnimation即可将其添加到视图上。 得益于此,ThinkVerb可以使用...

    iOS 性能优化

    CoreAnimation的基础 CoreAnimation利用了硬件加速和架构上的优化来实现了快速渲染和实时动画。当视图的drawRect方法首次被调用时,层会将描画的结果捕捉到一个位图中,并在随后的重绘时,尽可能使用这个位图,以...

    iOS核心动画高级技巧

    Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做CAlayer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一角。 Core ...

    iOS-Core-Animation-Advanced-Techniques.zip

    iOS 动画原理基础

    ios核心动画高级技巧中文版

    Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一 角。Core ...

    ios核心动画高级技巧

    Core Animation其实是一个令人误解的命名。你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一 角。Core ...

    YTAnimation:iOS动画集锦, swift, 核心动画, 基础动画,关键帧动画, 组动画, 过渡动画, 进度条,项目案例

    iOS 动画主要是指 Core Animation 框架, Core Animation是 iOS 和 OS X 平台上负责图形渲染与动画的基础框架。Core Animation 可以作用于动画视图或者其他可视元素,可以完成动画所需的大部分绘帧工作。Core ...

    ios-最全动画教程-树形动画.zip

    Core Animation核心动画基础教程学习实例之实现简单的树形动画效果 ! 1、学习IOS动画不错的教程,附上github地址: https://github.com/AsTryE/Core-Animation-Demo01 2、IOS动画分类基础知识,github地址: ...

Global site tag (gtag.js) - Google Analytics