CoreAnimation编程指南(十)KVC
2013-09-27 21:49
218 查看
CAAnimation和CALayer类扩展了NSKeyValueCoding协议,给键添加默认值,扩展了封装协议,支持CGPoint、CGRect、CGSize和CATransform3D关键路径。
CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值。即使键为“someKey”对应的属性没有被定义,你也可以给“someKey”的键设置一个值,如下:
MacOSX注意:在MacOSX上面,CALayer和CAAnimation类支持NSCoding协议,会自动归档这些你设置的额外键。
核心动画添加的键值编码约定,允许一个类在被使用时键没有被设置相应值的时候提供默认值。CALayer或CAAnimation支持该约定,通过使用方法defaultValueForKey:。
为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。
Listing1ExampleimplementationofdefaultValueForKey:
当使用键值编码方法访问属性,而属性的值不支持标准键-值编码封装约定的对象(NSObject)时候,你可以使用如下的封装约定:
CAAnimation提供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。
CATransform3D公开如下的字段:
CGPoint公开如下字段:
CGSize公开如下字段:
CGRect公开如下字段:
你不可以通过Objective-C2.0的属性方法来指定一个结构字段的关键路径。如下的代码是无法正常执行的:
1.1键-值编码兼容的容器类
CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值。即使键为“someKey”对应的属性没有被定义,你也可以给“someKey”的键设置一个值,如下:[theLayersetValue:[NSNumbernumberWithInteger:50]forKey:@"someKey"];
你可以通过下面的代码检索“someKey”对应的值:
someKeyValue=[theLayervalueForKey:@"someKey"];
MacOSX注意:在MacOSX上面,CALayer和CAAnimation类支持NSCoding协议,会自动归档这些你设置的额外键。
1.2支持默认值
核心动画添加的键值编码约定,允许一个类在被使用时键没有被设置相应值的时候提供默认值。CALayer或CAAnimation支持该约定,通过使用方法defaultValueForKey:。为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。
Listing1ExampleimplementationofdefaultValueForKey:
+(id)defaultValueForKey:(NSString*)key { if([keyisEqualToString:@"masksToBounds"]) return[NSNumbernumberWithBool:YES]; return[superdefaultValueForKey:key]; }
1.3封装约定
当使用键值编码方法访问属性,而属性的值不支持标准键-值编码封装约定的对象(NSObject)时候,你可以使用如下的封装约定:CType | Class |
CGPoint | NSValue |
CGSize | NSValue |
CGRect | NSValue |
CGAffineTransform | NSAffineTransform(MacOSXonly) |
CATransform3D | NSValue |
1.4支持结构字段的关键路径
CAAnimation提供支持使用关键路径访问选择的结构字段。这在为动画关键路径指定结构字段的时候非常有帮助,同时你可以使用setValue:forKeyPath:和valueForKeyPath来设置和读取相应的值。CATransform3D公开如下的字段:
StructureField | Description |
rotation.x | Therotation,inradians,inthexaxis. |
rotation.y | Therotation,inradians,intheyaxis. |
rotation.z | Therotation,inradians,inthezaxis. |
rotation | Therotation,inradians,inthezaxis.Thisisidenticaltosettingtherotation.zfield. |
scale.x | Scalefactorforthexaxis. |
scale.y | Scalefactorfortheyaxis. |
scale.z | Scalefactorforthezaxis. |
scale | Averageofallthreescalefactors. |
translation.x | Translateinthexaxis. |
translation.y | Translateintheyaxis. |
translation.z | Translateinthezaxis. |
translation | Translateinthexandyaxis.ValueisanNSSizeorCGSize. |
StructureField | Description |
x | Thexcomponentofthepoint. |
y | Theycomponentofthepoint. |
StructureField | Description |
width | Thewidthcomponentofthesize. |
height | Theheightcomponentofthesize. |
StructureField | Description |
origin | TheoriginoftherectangleasaCGPoint. |
origin.x | Thexcomponentoftherectangleorigin. |
origin.y | Theycomponentoftherectangleorigin. |
size | ThesizeoftherectangleasaCGSize. |
size.width | Thewidthcomponentoftherectanglesize. |
size.height | Theheightcomponentoftherectanglesize. |
myLayer.transform.rotation.x=0;
相反你必须使用setValue:forKeyPath:或者valuForKeyPath:,如下:
[myLayersetValue:[NSNumbernumberWithInt:0]forKeyPath:@"transform.rotation.x"];
相关文章推荐
- 基于LGPL开源项目 Log4cpp安装与使用
- CoreAnimation编程指南(九)图层布局
- CoreAnimation编程指南(八)事务
- Web编程技术点摘录(四)
- Delphi7 Just In Time debugger 与VS冲突
- CoreAnimation编程指南(七)图层Action
- Eclipse的秘籍
- CoreAnimation编程指南(六)动画
- CoreAnimation编程指南(五)图层内容
- CoreAnimation编程指南(四)图层树结构
- struts2中select的使用
- CoreAnimation编程指南(三)几何变换
- javaFX实践-快递查询工具 part 1
- C++ 强制类型转换
- VS2010+Windows 2008部署网站及asp.net业务系统问题收集
- IIS7.5 配置 PHP 5.3.5
- Python Unicode与中文处理
- 对这几天鼓捣eclipse-hadoop插件做一个错误总结
- C++不能继承与实例化问题
- paip.c++读写ini文件.