Core Animation - 4

前面的Core Animaiton 2 - 3的文章将CALayer简单了解了一下,现在重新进入官方文档的学习。

Core Animation Basics

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 提供了一个通用系统给一些app的视图和其他可视化元素来创建动画操作。Core Animation并不是应用视图的代替品。反而,是一种技术来整合视图并且提供更好的性能和支持相关内容的动画演示。它实现这些行为是通过把它们的视图的内容缓存成位图文件并且直接被图形硬件所操作。在一些情况中,这些缓存行为将会要求你重新考虑你如何呈现和管理你应用的内容,但是大多数时间你在使用Core Animation却不知道它的存在。出了缓存视图内容,Core Animation也定义了一种方式来确定任意的可视化内容,用你的视图对象来整合这些内容,并且和任何东西组合形成动画操作。

你使用Core Animation来动画演示一些应用视图和可视化内容的变化。大多数改变与修改你的可视化对象的属性有关。例如,你可以使用Core Animaiton来动画演示视图的position,size或者opacity。当你做了如此的改变,Core Animation将当前值和新确定的值用动画演示出来。


Layers Provide the Basis for Drawing and Animations

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.

层CALayer是核心动画的最基础的东西了。


Core Animation如何绘制内容,首先将layer object层对象处理:Backing Store,Hardware manipulation,Hardware Compositor

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 because it is done using the CPU on the main thread. Core Animation avoids this expense by whenever possible by manipulating the cached bitmap in hardware to achieve the same or similar effects.

Although Core Animation uses cached content as much as possible, your app must still provide the initial content and update it from time to time. There are several ways for your app to provide a layer object with content, which are described in detail in Providing a Layer’s Contents.

因为操作对象是静态位图,基于层的绘制与基于传统的视图的技术绘制有着非常大的区别。用基于视图的绘制,视图的改变通常要调用视图的drawRect:方法并用新参数来重绘内容。但是用这种方式绘制图像代价是非常昂贵的,因为这会在主线程使用CPU。Core Animaiton避免了这种花销用尽可能的由硬件位图缓存来操作来实现的同样的效果。

尽管Core Animation尽可能地使用缓存内容,你的应用仍需要提供初始化内容并且是不是地更新。这里有一些方法来提供你的应用的层对象的内容。

从上面这段话,知道,基于层对象的视图绘制与基于视图对象的绘制大大不同,一个效率高(位图缓存),一个效率低(主线程CPU使用)


Layer-Based Animations

The data and state information of a layer object is decoupled from the visual presentation of that layer’s content onscreen. This decoupling gives Core Animation a way to interpose itself and animate the change from the old state values to new state values. For example, changing a layer’s position property causes Core Animation to move the layer from its current position to the newly specified position. Similar changes to other properties cause appropriate animations. Figure 1-2 illustrates a few of the types of animations you can perform on layers. For a list of layer properties that trigger animations, see Animatable Properties

前面讲的有基于层的绘制 Layer-based  Drawing,当然有基于层的动画了:Layer-Based Animations。紫色字体讲的是层对象的数据和状态信息是被层对象内容在屏幕上可视化的呈现所解耦的,说白了就是层对象的属性的data数值和属性等变化的表现是可以用动画触发的。


移动,缩放,旋转,透明度的变化,边角的矩圆,背景色的改变都是常见的操作。很清楚见到,image对象下还有一个默认矩形的层对象(容器)

During the course of an animation, Core Animation does all of the frame-by-frame drawing for you in hardware.All you have to do is specify the start and end points of the animation and let Core Animation do the rest.You can also specify custom timing information and animation parameters as needed; however, Core Animation provides suitable default values if you do not.


Layer Objects Define Their Own Geometry

One of the jobs of a layer is to manage thevisual geometry for its content. The visual geometry encompasses information about the bounds of that content, its position on the screen, and whether the layer has been rotated, scaled, or transformed in any way. Like a view, a layer has frame and bounds rectangles that you can use to position the layer and its content. Layers also have other properties that views do not have, such as ananchor point, which defines the point around which manipulations occur. The way you specify some aspects of layer geometry also differs from how you specify that information for a view.

学习CALayer的时候要特别留心Geometry这个联系。


Layers Use Two Types of Coordinate Systems

Layers make use of both point-based coordinate systems andunit coordinate systems to specify the placement of content. Which coordinate system is used depends on the type of information being conveyed. Point-based coordinates are used when specifying values that map directly to screen coordinates or must be specified relative to another layer, such as for the layer’sposition property. Unit coordinates are used when the value should not be tied to screen coordinates because it is relative to some other value. For example, the layer’s anchorPoint property specifies a point relative to the bounds of the layer itself, which can change.

Among the most common uses for point-based coordinates is to specify the size and position of the layer, which you do using the layer’s bounds and position properties. Thebounds defines the coordinate system of the layer itself and encompasses the layer’s size on the screen. Theposition property defines the location of the layer relative to its parent’s coordinate system. Although layers have aframe property, that property is actually derived from the values in thebounds and position properties and is used less frequently.

The orientation of a layer’sbounds and frame rectangles always matches the default orientation of the underlying platform. Figure 1-3 shows the default orientations of the bounds rectangle on both iOS and OS X. In iOS, the origin of the bounds rectangle is in the top-left corner of the layer by default, and in OS X it is in the bottom-left corner. If you share Core Animation code between iOS and OS X versions of your app, you must take such differences into consideration.

留意到这个point-based coordinate systems 和 unit coordinate systems 这两个系统,

层对象使用两个坐标系统:point-based coordinate systems 和 unit coordinate systems 来确定内容的放置。哪一个坐标体系会被使用取决于被传送的信息的类型。point-based坐标系统会被使用当确定的值这直接导航了屏幕的坐标系统或者必须确定好与其他层的关系,例如层的position属性。Unit coordinates会被使用当值不应该被屏幕体系所绑定因为它关系到其他的值。例如,层的anchorPoint属性确定了一个点雨bounds的关系,这可以改变。

这段话介绍了一些关于坐标体系结构的介绍。


这个coordinate也是为了layer 的geometry所服务,注意到,iOS和Mac OS 还是不一样的哦


One thing to note in Figure 1-3 is that the position property is located in the middle of the layer. That property is one of several whose definition changes based on the value in the layer’s anchorPoint property. The anchor point represents the point from which certain coordinates originate and is described in more detail in Anchor Points Affect Geometric Manipulations.

The anchor point is one of several properties that you specify using the unit coordinate system. Core Animation uses unit coordinates to represent properties whose values might change when the layer’s size changes. You can think of the unit coordinates as specifying a percentage of the total possible value. Every coordinate in the unit coordinate space has a range of 0.0 to 1.0. For example, along the x-axis, the left edge is at the coordinate 0.0 and the right edge is at the coordinate 1.0. Along the y-axis, the orientation of unit coordinate values changes depending on the platform, as shown in Figure 1-4.

这里介绍了anchorPoint的意义,这个自己去了解了。


All coordinate values, whether they are points or unit coordinates are specified asfloating-point numbers. The use of floating-point numbers allows you to specify precise locations that might fall between normal coordinate values. The use of floating-point values is convenient, especially during printing or when drawing to a Retina display where one point might be represented by multiple pixels. Floating-point values allow you to ignore the underlying device resolution and just specify values at the precision you need.


Anchor Points Affect Geometric Manipulations

Geometry related manipulations of a layer occur relative to that layer’s anchor point, which you can access using the layer’s anchorPoint property. The impact of the anchor point is most noticeable when manipulating the position or transform properties of the layer. The position property is always specified relative to the layer’s anchor point, and any transformations you apply to the layer occur relative to the anchor point as well.

Figure 1-5 demonstrates how changing the anchor point from its default value to a different value affects the position property of a layer. Even though the layer has not moved within its parents’ bounds, moving the anchor point from the center of the layer to the layer’s bounds origin changes the value in the position property.



关于anchorPoint和position,前面的博客已经说的非常清楚了。

Figure 1-6 shows how changing the anchor point affects transforms applied to the layer. When you apply a rotation transform to the layer, the rotations occur around the anchor point. Because the anchor point is set to the middle of the layer by default, this normally creates the kind of rotation behavior that you would expect. However, if you change the anchor point, the results of the rotation are different.




这里要注意的是anchorPoint和 rotation transform 形变的联系!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值