Core Animation和CALayer

Animation Core想必大家都比较熟悉,是苹果一套针对动画效果的牛逼API,直接作用在layer上。今天打算扯扯这套API的某些细节,既然是作用在layer上那咱就先从layer开始扯起……

一、CALayer

1.1 什么是CALayer

每个能在界面上看到的ui控件,其实都是因为这些ui控件里面有个layer层。这个层就是CALayer类,它有个很牛逼的方法:- (void)drawInContext:(CGContextRef)ctx;从这就能看出,当UIView需要显示的时候就会调用它进行绘图,绘图完毕后系统会把这个层拷贝到屏幕上就会被你look到。它还有个牛逼的方法:- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key;这个就是跟Animation Core相关的方法,咱一会在看

1.2 CALayer怎么用?(CALayer是定义在QuartzCore框架下的)

/** Layer creation and initialization. **/

+ (instancetype)layer;

/* The designated initializer. */

- (instancetype)init;

/* This initializer is used by CoreAnimation to create shadow copies of
 * layers, e.g. for use as presentation layers. Subclasses can override
 * this method to copy their instance variables into the presentation
 * layer (subclasses should call the superclass afterwards). Calling this
 * method in any other situation will result in undefined behavior. */

- (instancetype)initWithLayer:(id)layer;

CALayer提供了这么多创建的方法。一个UIView默认会有一个Layer,但你可以手动添加其他的layer,可以设置或修改layer的属性改变起现实的样子。这些基础的东西直接贴段代码以示说明

CALayer *customLayer=[CALayer layer];
    customLayer.bounds=CGRectMake(10, 10, 100, 100);

    customLayer.backgroundColor=[UIColor orangeColor].CGColor;

    //设置层中心点的位置(默认情况下中心点位置为0,0)
    customLayer.position=CGPointMake(100, 100);

    //设置圆角半径
    customLayer.cornerRadius=10;
    [self.view.layer addSublayer:customLayer];

也可以让我们自己创建的层现实具体的内容,比如一张图片(设置它的contents属性)customLayer.contents=(id)[UIImage imageNamed:@"fish"].CGImage;

CGColorRef和UIColor以及CGImageRef和UIImage

上文已经提醒过CALayer是定义在QuartzCore框架里,而CGImageRef、CGColor是定义在CoreGraphics框架里的,UIColor、UIImage是定义在UIKit框架里的。QuartzCore和CoreGraphics是可以跨平台的,在Mac和iOS上都能使用,但UIKit只能在iOS中使用,故而为了可移植性,QuartzCore只能使用CGImageRef、CGColor。undenstand?

二、Core Animation

2.1 Core Animation基本使用

UIView *myView=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    myView.layer.position=CGPointMake(100, 100);
    myView.backgroundColor=[UIColor orangeColor];
    self.myView=myView;
    [self.view addSubview:myView];

    CABasicAnimation *anim=[CABasicAnimation animationWithKeyPath:@"position"];
    anim.duration=1.5;
    anim.fromValue=[NSValue valueWithCGPoint:CGPointMake(50, 80)];
    anim.toValue=[NSValue valueWithCGPoint:CGPointMake(300, 350)];
    anim.delegate=self;
    anim.removedOnCompletion=NO;
    anim.fillMode=kCAFillModeForwards;
    [myView.layer addAnimation:anim forKey:@"translate"];

两个需要注意的属性:

  1、toValue表示移动到某个位置,如果换成byValue就成了移动了多少位置

  2、默认情况下,动画执行完毕后会回到初始的位置,为了保持移动后的状态,需要设置

  anim.removedOnCompletion=NO;

anim.fillMode=kCAFillModeForwards;

CABasicAnimation是CAPropertyAnimation的自类,CAPropertyAnimation又是CAAnimation的子类,在CAAnimation中,在这个父类中有个代理其中有两个代理方法

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

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

可以用来监控运动的完成过程。在animationDidStop:方法中打印uiview当前的位置,可以看到与运动前的位置并没有改变。这是为什么呢?

2.2 Core Animation维护两个layer

这里需要说明的是当你把一个动画添加到layer时,是不直接修改它的属性的

Core Animation维护了两个平行的layer层次结构:model layer tree和presentation layer tree(模型层树和表示层书树)。前者中的 layers 反映了我们能直接看到的 layers 的状态,而后者的 layers 则是动画正在表现的值的近似。

/* Returns a copy of the layer containing all properties as they were

* at the start of the current transaction, with any active animations

* applied. This gives a close approximation to the version of the layer

* that is currently displayed. Returns nil if the layer has not yet

* been committed.

*

* The effect of attempting to modify the returned layer in any way is

* undefined.

*

* The `sublayers‘, `mask‘ and `superlayer‘ properties of the returned

* layer return the presentation versions of these properties. This

* carries through to read-only layer methods. E.g., calling -hitTest:

* on the result of the -presentationLayer will query the presentation

* values of the layer tree. */

- (id)presentationLayer;

/* When called on the result of the -presentationLayer method, returns

* the underlying layer with the current model values. When called on a

* non-presentation layer, returns the receiver. The result of calling

* this method after the transaction that produced the presentation

* layer has completed is undefined. */

- (id)modelLayer;

上面说到默认情况下动画结束后会被移除,也就是说动画不会在超出起持续时间后还修改presentation layer,而是会彻底移除,一旦动画被移除presentation layer将回到model layer的值,并且动画过程中我们并未修改过position的值,所以你看到它又回到了原来的位置

除了设置fillMode=kCAFillModeForward强制其留在最终状态并设置removedOnCompletion为NO以防止动画被自动移除外,还可以干脆就设置uiview的position为动画停止的position。

还有一点不是很多人了解的是:如果将已完成的动画保持在layer上时,会造成额外的开销,因为渲染器会去进行额外的绘画工作。庆幸的是我们创建的动画对象被添加到layer时立刻就赋值了一份。这个特性在多个view中重用动画时非常有用。

待续……

时间: 2024-08-30 15:36:52

Core Animation和CALayer的相关文章

iOS Core Animation之CALayer心得

使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的API是较高级的封装,使用便捷,使得我们免于自己使用OpenGL实现动画.本文主要介绍如何使用CALayer的mask实现一个双向注水动画(姑且这么叫吧). 了解CALayer的mask 以上是CALayer的头文件关于mask的说明,mask实际上layer内容的一个遮罩. 如果我们把mask是透明的

Core Animation学习总结

目录: The Layer Beneath The Layer Tree(图层树) The Backing Image(寄宿层) Layer Geometry(图层几何学) Visual Effects(视觉效果) Transforms(变换) Specialized Layers(专有图层) Setting Things in Motion Implicit Animations(隐式动画) Explicit Animations(显式动画) Layer Time(图层时间) Easing(缓

iphone弹出窗口效果的制作(Core animation, CALayer)

效果类似人人网微薄客户端的弹出效果 static CGFloat kTransitionDuration = 0.3; - (void)initView { UIWindow *window = [UIApplication sharedApplication].keyWindow; if (!window) { window = [[UIApplication sharedApplication].windows objectAtIndex:0]; } _backgroundView = [[

IOS Core Animation Advanced Techniques的学习笔记(五)

第六章:Specialized Layers   类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradientLayer 用于绘制一个颜色渐变填充图层的形状(所有圆角矩形边界内的部分) CAEAGLLayer/CAOpenGLLayer 用于设置需要使用OpenGL ES(iOS)或OpenGL(OS X)绘制的内容与内容储备. CAReplicatorLayer 当你想自动生成一个或多个子层的拷贝.复制器

使用 Core Animation 实现图片的碎片化----

用 Core Animation 实现图片的碎片化 参考书籍: 效果如下: 原理其实非常简单哦:). 1. 创建一个CALayer,使用其 contents 属性来装载一张图片(获取图片的CGImage) 2. 根据frame值裁剪图片,然后将裁剪的图片赋给你创建的更小的CALayer 3. 实现这些更小的CALayer的动画 4. 剩下的该干嘛干嘛,比如使用 Core Image 滤镜什么的,就靠你创造了:) 核心代码: 源码(书中提供,并非本人所写): /*** * Excerpted fr

iOS Core Animation Advanced Techniques(一):图层树、寄宿图以及图层几何学

(一)图层的树状结构 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只是Core Animation特性的冰山一角. Core Animation是一个复合引擎,它的职责就是尽可能快地组合屏幕上不同的可视内容,这个内容是被分解成独立的图层,存储在一个叫做图层树的体系之中.于是这个树形成了UIKit以及在iO

Core Animation 动画效果介绍

在开始之前呢,先了解一下UIView和CALayer大体的区别(重点列举了以下四点): UIView继承自 UIResponder,因此UIView 可以处理响应事件,而CALayer继承自NSObject,所以它只是负责内容的创建,绘制. UIView负责对内容的管理,而CALayer则是对内容的绘制 UIView中有关位置的属性只有frame.bounds.center,而CALayer除了具备这些属性之外还有anchorPoint.position. 通过修改CALayer可以实现UIVi

iOS Core Animation Advanced Techniques-隐式动画

上六章节: 图层树 图层的寄宿图 图层几何学 图层视觉效果 图层变换 专用图层 这篇随笔主要介绍有关图层隐式动画. 隐式动画: 没有指定任何动画类型,而改变了一个属性,Core Animation决定如何并且何时去做动画. 动画执行的事件取决于当前事务的设置: 动画类型取决于图层行为. Core Animation假设屏幕上任何东西都可能做动画,默认动画效果是打开的. 当CALayer的一个 可做动画的 属性 被改变,默认从先前值平滑过渡到新值,而不是立刻显示新值在屏幕上,因此携带了隐式动画.

iOS Core Animation Advanced Techniques-显式动画

上七章节: 图层树 图层的寄宿图 图层几何学 图层视觉效果 图层变换 专用图层 隐式动画 这篇随笔主要介绍有关图层显式动画. 显示动画: 能对一些属性做指定的自定义动画,或者创建非线性动画 属性动画: 属性动画作用于图层的某个单一属性,并指定了它的一个目标值,或一连串将要做动画的值 属性动画分两种: 1.基础 2.关键帧 基础动画:(通过CALayer的实例方法addAnimation: forKey:给图层添加显示动画) CABasicAnimation-->CAPropertyAnimati