Core Animaiton - 5

接上Core Animation - 4:

Layers Can Be Manipulated in Three Dimensions

Every layer has two transform matrices that you can use to manipulate the layer and its contents. The transform property of CALayer specifies the transforms that you want to apply both to the layer and its embedded sublayers. Normally you use this property when you want to modify the layer itself. For example, you might use that property to scale or rotate the layer or change its position temporarily. The sublayerTransform property defines additional transformations that apply only to the sublayers and is used most commonly to add a perspective visual effect to the contents of a scene.

Transforms work by multiplying coordinate values through a matrix of numbers to get new coordinates that represent the transformed versions of the original points. Because Core Animation values can be specified in three dimensions, each coordinate point has four values that must be multiplied through a four-by-four matrix, as shown in Figure 1-7. In Core Animation, the transform in the figure is represented by the CATransform3D type. Fortunately, you do not have to modify the fields of this structure directly to perform standard transformations. Core Animation provides a comprehensive set of functions for creating scale, translation, and rotation matrices and for doing matrix comparisons. In addition to manipulating transforms using functions, Core Animation extends key-value coding support to allow you to modify a transform using key paths. For a list of key paths you can modify, see CATransform3D Key Paths.


这里提到的变换矩阵是由struct CATransform3D提供:

typedef struct CATransform3D CATransform3D;

现在就好理解为什么这个结构体是这样的类型了。

上面提到的 CATransform3D Key Paths 是利用KVC的一种方便的代码形式。

该变换矩阵的取值规则如下:( 4*4 )


对应到矩阵的写法:



学习CATransform3D这个较为复杂的类可以先了解一个非常相似的由QuartzCore框架提供的CGAffineTransfrom,这里附上我之前写的一篇关于CGAffineTransfrom学习的一篇博客地址:http://blog.csdn.net/u013743777/article/details/47056467 

有了上面的理解,你可以知道,说白了,CATransform3D也就是平移缩放旋转等的更深层的操作罢了

而UIView对应着CGAffineTransform,而CALayer对应着CATransform3D这个属性。显然CATransform3D有着更强大,更不可思议的能力!

CALayer关于这个类有两个属性:

主层(及层上的子层)的属性:

/* A transform applied to the layer relative to the anchor point of its
 * bounds rect. Defaults to the identity transform. Animatable. */

@property CATransform3D transform;
子层的属性:

/* A transform applied to each member of the `sublayers' array while
 * rendering its contents into the receiver's output. Typically used as
 * the projection matrix to add perspective and other viewing effects
 * into the model. Defaults to identity. Animatable. */

@property CATransform3D sublayerTransform;

同样地iOS提供了一系列非常简单的接口方法:

定值的操作:

基于定值的平移:

/* Returns a transform that translates by '(tx, ty, tz)':
 * t' =  [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */

CA_EXTERN CATransform3D CATransform3DMakeTranslation (CGFloat tx,
    CGFloat ty, CGFloat tz)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

基于定值的缩放:

/* Returns a transform that scales by `(sx, sy, sz)':
 * t' = [sx 0 0 0; 0 sy 0 0; 0 0 sz 0; 0 0 0 1]. */

CA_EXTERN CATransform3D CATransform3DMakeScale (CGFloat sx, CGFloat sy,
    CGFloat sz)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

基于定值的旋转:

/* Returns a transform that rotates by 'angle' radians about the vector
 * '(x, y, z)'. If the vector has length zero the identity transform is
 * returned. */

CA_EXTERN CATransform3D CATransform3DMakeRotation (CGFloat angle, CGFloat x,
    CGFloat y, CGFloat z)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

基于原有值的叠加操作:

基于原有值的平移:

/* Translate 't' by '(tx, ty, tz)' and return the result:
 * t' = translate(tx, ty, tz) * t. */

CA_EXTERN CATransform3D CATransform3DTranslate (CATransform3D t, CGFloat tx,
    CGFloat ty, CGFloat tz)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

基于原有值的缩放:

/* Scale 't' by '(sx, sy, sz)' and return the result:
 * t' = scale(sx, sy, sz) * t. */

CA_EXTERN CATransform3D CATransform3DScale (CATransform3D t, CGFloat sx,
    CGFloat sy, CGFloat sz)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

基于原有值的旋转:

/* Rotate 't' by 'angle' radians about the vector '(x, y, z)' and return
 * the result. If the vector has zero length the behavior is undefined:
 * t' = rotation(angle, x, y, z) * t. */

CA_EXTERN CATransform3D CATransform3DRotate (CATransform3D t, CGFloat angle,
    CGFloat x, CGFloat y, CGFloat z)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

对上面几个稍作解释:

例如:

/* Returns a transform that translates by '(tx, ty, tz)':
 * t' =  [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */

CA_EXTERN CATransform3D CATransform3DMakeTranslation (CGFloat tx,
    CGFloat ty, CGFloat tz)
    __OSX_AVAILABLE_STARTING (__MAC_10_5, __IPHONE_2_0);

返回一个变换,由'(tx, ty, tz)'提供的值来平移:

变换参数为:[ 1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1 ] 对应到刚好上面的例子:

观察这个矩阵,可以很明显看到对角线都为1,如果数学好的很容易就会理解,经过运算后,对象本身的bounds尺寸是不会变化的,但是坐标的三个对应系xyz则会相应地平移,所以此为一个平移操作,并且是基于3D的,很好理解吧!

同理于缩放和旋转也一样!


不管哪个语言,其实UI对应的都是归于图形界面的一大类,而图形界面牵涉到数学基础理论一些基础的图形矩阵,变换矩阵都是很基本的,很常识的,但是一个小小的操作都要带来很大的运算量,更别说组合的炫酷图形操作了,但是在软件业高度发展的现在,iOS早就为开发者封装好了这一切,所以我们只用调调API就可以轻松实现原本含杂着大量数学运算的效果!

建议先学习CGAffineTransform,再来学习CATransform就会发现大同小异,很好理解了!


参考至:

http://blog.sina.com.cn/s/blog_8f5097be0101b91z.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值