iOS CAAnimation(三)核心动画基础

本文详细介绍了iOS中的Core Animation,它是一个用于动画化应用视图和其他视觉元素的图形渲染和动画基础设施。Core Animation自动处理大部分动画帧的绘制工作,提供高性能和流畅的动画。核心在于layer对象,用于管理内容和操作。文章探讨了layer的特性、如何为layer提供内容、调整其属性,并总结了layer的重要地位及其在动画中的作用。
摘要由CSDN通过智能技术生成

layer在核心动画中的地位:

先看一下这个经典的结构图,Core Animation的位置在UIKit/AppKit层之下。

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 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. 

 

引用官方文档里对Core Animation的介绍来看。我们可以使用核心动画使我们app中的视觉元素动起来,而且大部分绘制工作已经为我们准备好了,我们只需要配置一些参数即可,其余部分将由Core Animation完成,它将大部分图形绘制工作交给图形硬件以加快渲染速度。这种自动图形加速功能可实现高帧率和流畅的动画效果,而不会给CPU造成负担并降低app的运行速度。  

Core Animation本身不是绘制系统,它是在硬件中合成和操作应用content的基础组件,而这个基础组件的核心是layer层,我们可以用layer来管理和操作content。layer会捕获content并生成位图(Bitmap),这样图形硬件就可以轻松地利用生成的位图(Bitmap)去做一些绘制工作。

Core Animation provides a general purpose system for animating views and other visual elements of your app. Core Animation is not a replacement for your app’s views. Instead, it is a technology that integrates with views to provide better performance and support for animating their content. It achieves this behavior by caching the contents of views into bitmaps that can be manipulated directly by the graphics hardware. In some cases, this caching behavior might require you to rethink how you present and manage your app’s content, but most of the time you use Core Animation without ever knowing it is there. In addition to caching view content, Core Animation also defines a way to specify arbitrary visual content, integrate that content with your views, and animate it along with everything else.

You use Core Animation to animate changes to your app’s views and visual objects. Most changes relate to modifying the properties of your visual objects. For example, you might use Core Animation to animate changes to a view’s position, size, or opacity. When you make such a change, Core Animation animates between the current value of the property and the new value you specify. You would typically not use Core Animation to replace the content of a view 60 times a second, such as in a cartoon. Instead, you use Core Animation to move a view’s content around the screen, fade that content in or out, apply arbitrary graphics transformations to the view, or change the view’s other visual attributes.

Core Animation通过将视图的内容缓存到可以由图形硬件直接操作的位图(Bitmap)中来实现此行为。

建议不要使用Core Animation进行每秒60次替换视图内容的工作,比如说卡通。而是使用Core Animation在屏幕上移动视图的内容,淡入或淡出该内容,对视图应用任意图形转换或更改视图的其他视觉属性。

既然说layer是核心内容,那我们来看看官方文档中关于layer的说法:

Layer objects are 2D surfaces organized in a 3D space and are at the heart of everything you do with Core Animation. Like views, layers manage information about the geometry, content, and visual attributes of their surfaces. Unlike views, layers do not define their own appearance. A layer merely manages the state information surrounding a bitmap. The bitmap itself can be the result of a view drawing itself or a fixed image that you specify. For this reason, the main layers you use in your app are considered to be model objects because they primarily manage data. This notion is important to remember because it affects the behavior of animations.

这里再次重申了layer是我们使用Core Animation的核心,layer管理着bitmap的状态信息。

Most layers do not do any actual drawing in your app. Instead, a layer captures the content your app provides and caches it in a bitmap, which is sometimes referred to as the backing store. When you subsequently change a property of the layer, all you are doing is changing the state information associated with the layer object. When a change triggers an animation, Core Animation passes the layer’s bitmap and state information to the graphics hardware, which does the work of rendering the bitmap using the new information, as shown in Figure 1-1. Manipulating the bitmap in hardware yields much faster animations than could be done in software.

大多数layer不会进行实际绘制工作,它们只是捕获当前应用的content并缓存为bitmap。

当我们改变layer的属性时,我们只是在改变layer的状态信息。

当改变触发了动画,CoreAnimation会将 layer缓存起来的bitmap 和 关联的状态信息 交给图形硬件,图形硬件会用新的状态信息对bitmap进行渲染。直接在硬件中对bitmap进行操作比在软件中完成同样的任务要快的多。

Because it manipulates a static bitmap, layer-based drawing differs significantly from more traditional view-based drawing techniques. With view-based drawing, changes to the view itself often result in a call to the view’s drawRect: method to redraw content using the new parameters. But drawing in this way is expensive

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值