32.核心动画介绍04-27

前言:
任何复杂的动画其实都是由一个个简单的动画组装而成的,只要我们善于分解和组装,我们就能实现出满意的效果。
一、简介
iOS动画主要是指Core Animation(我们简称CA)框架。官方使用文档地址为:Core Animation Guide

二、开始解读官方文档
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.

Core Animation是iOS和macOS平台上负责图形渲染与动画的基础框架。Core Animation可以作用与动画视图或者其他可视元素,为你完成了动画所需的大部分绘帧工作。你只需要配置少量的动画参数(如开始点的位置和结束点的位置)即可使用Core Animation的动画效果。Core Animation将大部分实际的绘图任务交给了图形硬件来处理,图形硬件会加速图形渲染的速度。这种自动化的图形加速技术让动画拥有更高的帧率并且显示效果更加平滑,不会加重CPU的负担而影响程序的运行速度。

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.

如果你正在编写iOS应用程序,你可以使用的是核心动画,不管你是否知道。如果你正在编写OSX应用程序,你可以很轻易地使用核心动画。核心动画位于AppKIT和UIKit的下方,并集成到Cocoa和Cocoa Touch的视图工作流程中。当然,核心动画还具有扩展应用程序视图所显示的功能的接口,并为您的应用程序动画提供更精确的控制。


图片.png

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 infrastructur
看一眼
您可能永远不需要直接使用核心动画,但是当您这样做时,您应该理解核心动画作为应用程序基础结构的一部分所起的作用。

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.

核心动画管理你的app的内容
核心动画本身不是绘图系统。它是在硬件中合成和操作应用程序内容的基础设施。这个基础结构的核心是层对象,用来管理和操作内容。一个图层将你的内容捕获到一个可以被图形硬件轻松操纵的位图中。在大多数应用程序中,层是用来管理视图内容的一种方式,但也可以根据您的需要创建独立的层。

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.

修改层,触发动画
动画你使用核心动画创建的大部分动画,都涉及修改层的属性。像views,层对象有大小,有屏幕位置,不透明度,形变,和许多其他可修改的视觉导向的属性;对于这些属性,改变这些属性值可以产生动画的创建,通过修改旧值到新值。Changing the results in the creation of an implicit动画whereby the层animates from the old value to the new value。在你想要跟多控制动画行为的情况下,你也可以明确这些属性。

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.
层可以被组织成层次结构。
层次可以分层排列,以创建父子关系。层的排列影响视觉内容的管理方式,类似于views。连接到视图的一组层的层次结构反映了相应的视图层次结构。您还可以将独立的层添加到层层次结构中,以扩展您的应用程序的视觉内容,而不仅仅是您的视图。

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.
动作对象让你改变一个层的默认行为
隐式层动画是使用动作对象来实现的,这些动作对象是实现预定义接口的通用对象。核心动画使用动作对象来实现通常与层相关联的默认动画效果。您可以创建自己的动作对象来实现自定义动画,或者使用它们来实现其他类型的行为。然后,将动作对象分配给该层的属性之一。当该属性更改时,核心动画将检索您的操作对象并告诉它执行其动作。

ps:例如view缩小的动画执行万,会恢复原来的大小

How to Use This Document

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

怎么使用这个文档
这个文档是为那些需要更多控制他们的应用程序动画或想要使用层来提高他们的应用程序的绘图性能的开发者而设计的。该文档还提供了关于IOS和OS X的层和视图之间的集成的信息。在IOS和OS X之间,层和视图之间的集成是不同的,并且理解这些差异对于能够创建高效动画是重要的。

Prerequisites

You should already understand the view architecture of your target platform and be familiar with how to create view-based animations. If not, you should read one of the following documents:
您应该已经了解了目标平台的视图体系结构,并且熟悉如何创建基于视图的动画。如果没有,你应该阅读下列文件之一:

See Also

For examples of how to implement specific types of animations using Core Animation, see Core Animation Cookbook.
也可以看下
举例说明如何使用核心动画来实现特定类型的动画,请参见核心动画CooBook。

推荐阅读更多精彩内容