Core Animation - 1

来源:互联网 发布:无损剪切软件 编辑:程序博客网 时间:2024/04/29 06:47

接下来就进入到Core Animation 最重要而且相对难很多的核心动画框架的学习,一如既往,先从文档开始入手,这个是版本的文档:

This table describes the changes to Core Animation Programming Guide.

Date

Notes

2015-03-09

Added brief discussion of CAMetalLayer among possible layer classes.

2013-01-28

Major revamp, reorganization, and expansion to cover modern Core Animation behavior in iOS and OS X.

 

Incorporated the content of Animation Types and Timing Programming Guide into this document.

2010-09-24

Updated the document to reflect Core Animation support in iOS 4.2.

2010-08-12

Corrected iOS origin information. Clarified that the coordinate system origin used in the examples are based on the OS X model.

2010-05-25

Corrected autoresizing masks table.

2010-03-24

Added missing constant to the contentGravity property resizing table in Providing Layer Content.

2010-02-24

Updated Core Animation Kiosk Style Menu tutorial project.

2010-01-20

Updated infinite value for repeatCount.

2009-10-19

Modified section headings.

2009-08-13

Corrected availability of cornerRadius on iOS v 3.0 and later.

2008-11-13

Introduces iOS SDK content to OS X content. Corrects frame animation capabilities.

2008-09-09

Corrected typos.

2008-06-18

Updated for iOS.

2008-05-06

Corrected typos.

2008-03-11

Corrected typos.

2008-02-08

Corrected typos. Corrected RadiansToDegrees() calculation.

2007-12-11

Corrected typos.

2007-10-31

Added information on the presentation tree. Added example application walkthough.


先做好准备Core Animaiton非常复杂庞大, 先做好心里准备, 磨刀霍霍向前拼!


Core Animation is a graphics rendering and animation infrastructure available on both iOS and OS X that you use to animate the views and other visual elements of your app. With Core Animation, most of the work required to draw each frame of an animation is done for you. All you have to do is configure a few animation parameters (such as the start and end points) and tell Core Animation to start. Core Animation does the rest, handing most of the actual drawing work off to the onboard graphics hardware to accelerate the rendering. This automatic graphics acceleration results in high frame rates and smooth animations without burdening the CPU and slowing down your app.

If you are writing iOS apps, you are using Core Animation whether you know it or not. And if you are writing OS X apps, you can take advantage of Core Animation with extremely little effort. Core Animation sits beneath AppKit and UIKit and is integrated tightly into the view workflows of Cocoa and Cocoa Touch. Of course, Core Animation also has interfaces that extend the capabilities exposed by your app’s views and give you more fine-grained control over your app’s animations.

Core Animation是一个图形渲染和动画架构在iOS和OS X都可以使用你可以使用它来使得你视图或者其它可视化元素动画操作在你的应用上(很大区别了吧,既可以用在UIView上又可以应用在其他非UIView元素上)。有了Core Animation,大多数要求去绘制动画每一个帧的工作都帮你做好了。你要做的所有事情就是去配置一些动画需要的参数(例如开始或者结束点)并且告诉Core Animation去启动。Core Animation就会做剩下的事情,处理大多数实际的绘制工作来驱动载体图像硬件来加速渲染handing most of the actual drawing work off to the onboard graphics hardware to accelerate the rendering. 不知道咋翻译。这个自动的图形加速导致了一个高帧速率和一个平滑的动画效果而不用加大CPU的负载和拖慢app的速度。

如果你在编写iOS应用程序,你就在使用Core Animation不管你知不知道。如果你在编写OS X应用程序,你可以用一个机制的效果取得Core Animaiton的有利条件。Core Animaiton在AppKit和UIKit架构下并且整体地和Cocoa/Cocoa Touch视图流联系在一起。当然了,Core Animaiton也有一些被你应用的视图所暴露出来的接口可供使用(这个值得思考哦)并且给你一个更细粒度来控制你的动画操作(看好了,区别有这个,更加细粒度的)

这个架构图就非常醒目了,UIKit/AppKit框架下关于图像绘制的几个重要的核心框架: Core Animation , OpenGL ES/OpenGL , Core Graphics (Quartz),  Graphics Hardware。



At a Glance

You may never need to use Core Animation directly, but when you do you should understand the role that Core Animation plays as part of your app’s infrastructure.

你可能从来没有直接使用过Core Animaiton, 但是当你要用的时候你应该要理解一个关键的地方就是Core Animation扮演着你的应用程序的一个重要的架构基础。



Core Animation Manages Your App’s Content

Core Animation is not a drawing system itself. It is an infrastructure for compositing and manipulating your app’s content in hardware. At the heart of this infrastructure are layer objects, which you use to manage and manipulate your content. A layer captures your content into a bitmap that can be manipulated easily by the graphics hardware. In most apps, layers are used as a way to manage the content of views but you can also create standalone layers depending on your needs.

Core Animation并不是绘制系统本身。而是一个基础架构提供合成和管理应用的内容在硬件层上。这个架构的核心是layer对象,道layer 层的的重要啦!你使用layer层对象来管理和操纵你的内容。一个层对象将你的应用内容捕获成一个位图这个位图非常容易被图形硬件所操作。在大多数应用中,layers层都是作为视图的内容来使用但是你也可以单独创建一个层对象取决于你的需要。


Core Animation Basics基本准则和如何创建Layer对象都会在接下来的文档学习中提到。



Layer Modifications Trigger Animations

Most of the animations you create using Core Animation involve the modification of the layer’s properties. Like views, layer objects have a bounds rectangle, a position onscreen, an opacity, a transform, and many other visually-oriented properties that can be modified. For most of these properties, changing the property’s value results in the creation of an implicit animation whereby the layer animates from the old value to the new value. You can also explicitly animate these properties in cases where you want more control over the resulting animation behavior.

你用Core Animation所创建的大多数动画都牵涉到layer层属性的修改。如视图,层对象拥有矩形界面,一个屏幕位置,一个透明值,一个转换属性和许多其他面向可视化的属性可以被修改。对于大多数这些的属性,修改属性的值都会导致产生一个隐含的动画操作因此层的属性会从旧值动画地转化到新值。你也可以明确地将这些属性动画地改变在这种情况下你会对这些动画表现有着更多的控制。



Layers Can Be Organized into Hierarchies

Layers can be arranged hierarchically to create parent-child relationships. The arrangement of layers affects the visual content that they manage in a way that is similar to views. The hierarchy of a set of layers that are attached to views mirrors the corresponding view hierarchy. You can also add standalone layers into a layer hierarchy to extend the visual content of your app beyond just your views.

层对象可以被层次性地安排来创建父子的关系(这种关系和类的继承不一样哦,有没有想起很熟悉咧,对了就是:view-hierarchy)。这种对层的层级结构的安排影响了相关的可视化内容在这种方式下和视图的相关处理是非常相似的。依附在视图的层的集合的层级结构反映了相关的视图层级结构。(这里说的是layer作为UIView的属性来操作的时候,attach to View)。你也可以单独地创建layer并且相应地帮它们创建层级结构在视图之外来扩展你的应用的内容。




Actions Let You Change a Layer’s Default Behavior

Implicit layer animations are achieved using action objects, which are generic objects that implement a predefined interface. Core Animation uses action objects to implement the default set of animations normally associated with layers. You can create your own action objects to implement custom animations or use them to implement other types of behaviors too. You then assign your action object to one of the layer’s properties. When that property changes, Core Animation retrieves your action object and tells it to perform its action.

隐含的layer动画操作经常被用action 对象来实现(话说这个action对象是啥),这是一个通用的对象来实现一个预定义的接口。Core Animation 使用 action 对象来实现默认的动画设置通常和层对象有联系。你可以创建你自己的action对象来实现自定义的动画或者用它们来实现其他类型的行为。然后你分配你的action对象给其中一个层属性。当这个属性改变的时候,Core Animation检索你的 action对象并且告诉它执行这个action。



文档的总结:

This document is intended for developers who need more control over their app’s animations or who want to use layers to improve the drawing performance of their apps. This document also provides information about the integration between layers and views for both iOS and OS X. The integration between layers and views is different on iOS and OS X and understanding those differences is important to being able to create efficient animations.

就是说UIView和layer的整体的关系和区别非常重要啦,而且首先要先掌握了UIView的层级继承关系和动画操作的基础,之所以使用layer,更多地是站在提高性能的角度上。



接下来我们从一只围绕的最重要的一个核心入手:CALayer 

进入到接口文件可以看到非常醒目的一行:/* CoreAnimation - CALayer.h  */这么直截了当的关系,说明CALayer对于Core Animation的重要的性啦。

CALayer是Core Animaiton的核心不言而喻了,CALayer管理着视图的contents和提供了属性样式的修改和内容的显示。

CALayer是包含在QuartzCore这个框架文件中,Core Animaiton的原理开发动画的本质就是将CALayer中的内容转化为位图从而供硬件操作,从而提高动画的性能。

@interface CALayer : NSObject <NSCoding, CAMediaTiming>{@private  struct _CALayerIvars {    int32_t refcount;    uint32_t magic;    void *layer;#if TARGET_OS_MAC && !TARGET_RT_64_BIT    void *unused1[8];#endif  } _attr;}
可以见到又一个@private变量,这里的私有变量只有一个结构体:分别是两个32位的refcount(引用数?),magic变量,还有一个void*的层变量。

来看看官方文档是怎么说的:

The CALayer class manages image-based content and allows you to perform animations on that content. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen. Modifying the properties of the layer is how you initiate animations on the layer’s content or geometry. A layer object encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer’s timing information.

If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship. For layers you create yourself, you can assign a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. A layer may also have a layout manager object (assigned to thelayoutManager property) to manage the layout of subviews separately.


CALayer类管理一个基于图像内容并且允许你在这个内容中来执行动画操作。层对象经常是用来提供一种backing store(后备储存)给视图对象但也可以直接使用而没有UIView的基础。一个层的主要工作是来管理你提供的可视化内容但是层对象的本身也拥有可视化属性并且能够被设置,例如背景颜色,边框属性,和阴影。为了去管理可视化内容,层对象也可以维护一些关于它本身的几何图像的内容(例如它的位置,大小和变换形式),是一些用来在内容所在的屏幕呈现的属性。修改层对象的属性是代表了你如何来初始化一些在层对象的内容或者几何图形的动画操作。一个层对象封装了层的持久属性和pacing(不知道如何翻译)和关于它的一些采用了CAMediaTiming协议的动画,这定义了层对象的timing信息。

如果这个层对象是被视图创建的,这个视图会通常性地自动分配它给这个层的委托(就是UIView对象实现了CALayer的delegate),而且你也不应该改变这种关系。如果自己创建的层对象,你可以分配一个委托对象并且使用这个对象来动态地提供层的内容并且执行其他任务。一个层也应该拥有一个布局管理对象(分配给这个layoutManager property)来分离地管理层的子视图的布局。


官方文档分了下面的篇章来介绍,我们每一篇章选择一两个核心的API来学习,单独学习完API后在对比UIView的区别和联系后在附上一些典型的用法,估计要三个篇章的文章才能更完。

1.Creating a Layer    

CALayer提供了一个静态一个动态的初始化方法:

+ (instancetype)layer;- (instancetype)init;

还有一个designated initializer:

- (instancetype)initWithLayer:(id)layer;

2.Accessing Related Layer Objects    

提供了几个获取相关layer的方法,具体要用的时候查:

- (id)presentationLayer;- (id)modelLayer;

3.Accessing the Delegate  

@property(weak) id delegate
需要注意的是如果使用的是基于UIView的layer,iOS会自动把你讲layer的delegate赋给当前的UIView对象,并且不会也不建议修改这个关系。以后在详细介绍如何使用layer的delegate — CALayerDelegate。

4.Providing the Layer’s Content    

@property(strong) id contents;
提供了一个属性来控制layer的内容,当你使用图像对象的时候,这个时候大概会返回CGImageRef对象(这个与UIImage的区别,自行百度哈)。

还提供了几个相关的content方法。

5.Modifying the Layer’s Appearance

这个章节就比较大了,本来layer就属于UI范围的类,而涉及到Appearance的方法和属性更是很多,例如:contentsGravity(一段描述了layer相关的很多属性的字符串),opacity(透明值),hidden(是否隐藏),maskToBounds、mask、CornerRadius等(这几个很常见啦,以后再作详述)等等等属性。

6.Accessing the Layer’s Filters

layer的过滤器,这个是与图形学和图像有关的篇章,以后再做详述。

7.Configuring the Layer’s Rendering Behavior

配置层的渲染选项。Core Animation之所以高性能就是在randering 渲染的环节做了优化,所以这章肯定为一大重要点。

8.Modifying the Layer Geometry

这一章就比较有意思了,很多与UIView共有的属性,包括frame、bounds。不过也提供了很多UIView没有的相关与Geometry有关的属性。

9.Managing the Layer’s Transform

层的转换属性和方法。

10.Managing the Layer Hierarchy

管理层的层“继承“结构,这里可以类比一下UIView拥有的super和subview

@property(readonly) CALayer *superlayer;@property(copy) NSArray *sublayers;

11.Updating Layer Display

更新展示的方法和方式。

12.Layer Animations

层动画,不用说的,这肯定属于一个大重点了。

13.Managing Layer Resizing and Layout

层(子层)布局:Invalidates the layer’s layout and marks it as needing an update.

大量的和UIView相似的布局方法:

- (void)setNeedsDisplay;- (BOOL)needsDisplay;- (void)displayIfNeeded;- (void)setNeedsLayout;- (void)layoutIfNeeded;- (void)layoutSublayers;

14.Getting the Layer’s Actions

Returns the action object assigned to the specified key.   涉及到的Target-action设计模式,有时间一定要详细了解。

15.Mapping Between Coordinate and Time Spaces

貌似与坐标系有关的。

16.Hit Testing

Returns the farthest descendant of the receiver in the layer hierarchy (including itself) that contains the specified point.

返回在层继承结构中最下限的后代(包括自己),包含了一些特殊的点。。。看完了不知道什么鬼

17.Scrolling

The visible region of the layer in its own coordinate space. (read-only)

18.Identifying the Layer

层的名字。。。

19.Key-Value Coding Extensions

KVC,CALayer implements the standard NSKeyValueCoding protocol for all

20.Constants

一些相关常数的定义


一些属性的总结:




下一篇章就会进行上面的详细学习,come on!






0 0
原创粉丝点击