CoreAnimation编程指南(十)KVC
2015-07-15 11:25
218 查看
CAAnimation和CALayer类扩展了NSKeyValueCoding协议,给键添加默认值,扩展了封装协议,支持CGPoint、CGRect、CGSize和CATransform3D关键路径。
CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值。即使键为“someKey”对应的属性没有被定义,你也可以给“someKey”的键设置一个值,如下:
Mac OS X 注意:在Mac
OS X上面,CALayer和CAAnimation类支持NSCoding协议,会自动归档这些你设置的额外键。
核心动画添加的键值编码约定,允许一个类在被使用时键没有被设置相应值的时候提供默认值。CALayer或CAAnimation支持该约定,通过使用方法defaultValueForKey:。为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。Listing 1 Example implementation of defaultValueForKey:
当使用键值编码方法访问属性,而属性的值不支持标准键-值编码封装约定的对象(NSObject)时候,你可以使用如下的封装约定:
CAAnimation提供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。CATransform3D公开如下的字段:
CGPoint公开如下字段:
CGSize公开如下字段:
CGRect公开如下字段:
你不可以通过Objective-C 2.0的属性方法来指定一个结构字段的关键路径。如下的代码是无法正常执行的:
1.1 键-值编码兼容的容器类
CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值。即使键为“someKey”对应的属性没有被定义,你也可以给“someKey”的键设置一个值,如下:你可以通过下面的代码检索“someKey”对应的值:
OS X上面,CALayer和CAAnimation类支持NSCoding协议,会自动归档这些你设置的额外键。
1.2 支持默认值
核心动画添加的键值编码约定,允许一个类在被使用时键没有被设置相应值的时候提供默认值。CALayer或CAAnimation支持该约定,通过使用方法defaultValueForKey:。为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。Listing 1 Example implementation of defaultValueForKey:
1.3 封装约定
当使用键值编码方法访问属性,而属性的值不支持标准键-值编码封装约定的对象(NSObject)时候,你可以使用如下的封装约定:C Type | Class |
CGPoint | NSValue |
CGSize | NSValue |
CGRect | NSValue |
CGAffineTransform | NSAffineTransform(Mac OS X only) |
CATransform3D | NSValue |
1.4 支持结构字段的关键路径
CAAnimation提供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。CATransform3D公开如下的字段:Structure Field | Description |
rotation.x | The rotation, in radians, in the x axis. |
rotation.y | The rotation, in radians, in the y axis. |
rotation.z | The rotation, in radians, in the z axis. |
rotation | The rotation, in radians, in the z axis. This is identical to setting the rotation.z field. |
scale.x | Scale factor for the x axis. |
scale.y | Scale factor for the y axis. |
scale.z | Scale factor for the z axis. |
scale | Average of all three scale factors. |
translation.x | Translate in the x axis. |
translation.y | Translate in the y axis. |
translation.z | Translate in the z axis. |
translation | Translate in the x and y axis. Value is an NSSize or CGSize. |
Structure Field | Description |
x | The x component of the point. |
y | The y component of the point. |
Structure Field | Description |
width | The width component of the size. |
height | The height component of the size. |
Structure Field | Description |
origin | The origin of the rectangle as a CGPoint. |
origin.x | The x component of the rectangle origin. |
origin.y | The y component of the rectangle origin. |
size | The size of the rectangle as a CGSize. |
size.width | The width component of the rectangle size. |
size.height | The height component of the rectangle size. |
相反你必须使用setValue:forKeyPath:或者valuForKeyPath:,如下:
相关文章推荐
- Eclipse设置相同变量背景色高亮显示
- JDK动态代理实现原理
- ubuntu安装配置jdk
- 简单QT应用了可实现手动布局QT应用
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosin
- CoreAnimation编程指南(九)图层布局
- CoreAnimation编程指南(八)事务
- java.lang.OutOfMemoryError: PermGen space
- java static代码段 2
- Django中处理出错页面的方法
- CoreAnimation编程指南(七)图层Action
- Spring-Controller单元测试
- CoreAnimation编程指南(六)动画
- [LeetCode][Java] Spiral Matrix
- CoreAnimation编程指南(五)图层内容
- 如何在改版的spring官网下载jar包问题
- CoreAnimation编程指南(四)图层树结构
- IDEA - 自动添加代码
- CoreAnimation编程指南(三)几何变换
- C# Socket