CoreAnimation编程指南(十)KVC

本文深入探讨Core Animation中的键-值编码机制,解释如何使用CALayer和CAAnimation类进行属性修改与默认值设定。文章还详细介绍了封装约定、支持的结构类型及其关键路径使用方法,以及在不同平台上如何处理这些特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-10-kvc.html


CAAnimation和CALayer类扩展了NSKeyValueCoding协议,给键添加默认值,扩展了封装协议,支持CGPoint、CGRect、CGSize和CATransform3D关键路径。

1.1  键-值编码兼容的容器类

CALayer和CAAnimation都是键-值编码兼容的容器类,允许你修改属性键对应的值。即使键为“someKey”对应的属性没有被定义,你也可以给“someKey”的键设置一个值,如下:

        [theLayer setValue:[NSNumber numberWithInteger:50] forKey:@"someKey"];
你可以通过下面的代码检索“someKey”对应的值:
        someKeyValue=[theLayer valueForKey:@"someKey"];

    Mac OS X 注意:在Mac OS X上面,CALayer和CAAnimation类支持NSCoding协议,会自动归档这些你设置的额外键。

1.2 支持默认值

核心动画添加的键值编码约定,允许一个类在被使用时键没有被设置相应值的时候提供默认值。CALayer或CAAnimation支持该约定,通过使用方法defaultValueForKey:。

为了给键提供默认值,你创建相应的子类,并重载defaultValueForKey:。子类实现相应的键参数检查并返回适当的默认值。清单1描述了一个实现defaultValueForKey:的例子,它给masksToBounds提供新的默认值。

Listing 1  Example implementation of defaultValueForKey:

+ (id)defaultValueForKey:(NSString *)key
{
    if ([key isEqualToString:@"masksToBounds"])
         return [NSNumber numberWithBool:YES];
 
    return [super defaultValueForKey:key];
}
 

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.

CGPoint公开如下字段:

Structure Field

Description

x

The x component of the point.

y

The y component of the point.

CGSize公开如下字段:

Structure Field

Description

width

The width component of the size.

height

The height component of the size.

CGRect公开如下字段:

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.

你不可以通过Objective-C 2.0的属性方法来指定一个结构字段的关键路径。如下的代码是无法正常执行的:

    myLayer.transform.rotation.x=0;
相反你必须使用setValue:forKeyPath:或者valuForKeyPath:,如下:
    [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值