CoreAnimation编程指南(十)KVC
生活随笔
收集整理的這篇文章主要介紹了
CoreAnimation编程指南(十)KVC
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
CAAnimation和CALayer類擴展了NSKeyValueCoding協(xié)議,給鍵添加默認值,擴展了封裝協(xié)議,支持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協(xié)議,會自動歸檔這些你設置的額外鍵。
1.2 支持默認值
? 核心動畫添加的鍵值編碼約定,允許一個類在被使用時鍵沒有被設置相應值的時候提供默認值。CALayer或CAAnimation支持該約定,通過使用方法defaultValueForKey:。
? 為了給鍵提供默認值,你創(chuàng)建相應的子類,并重載defaultValueForKey:。子類實現(xiàn)相應的鍵參數(shù)檢查并返回適當?shù)哪J值。清單1描述了一個實現(xiàn)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)時候,你可以使用如下的封裝約定: ? CGPoint ? ? NSValue ? CGSize ? ? NSValue ? CGRect ? ? NSValue ? CGAffineTransform ? ? NSAffineTransform (Mac OS X only) ? CATransform3D ? ? NSValue
1.4 支持結(jié)構(gòu)字段的關鍵路徑
? CAAnimation提供支持使用關鍵路徑訪問選擇的結(jié)構(gòu)字段。這在為動畫關鍵路徑指定結(jié)構(gòu)字段的時候非常有幫助,同時你可以使用setValue:forKeyPath:和valueForKeyPath來設置和讀取相應的值。
? CATransform3D公開如下的字段: ? 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公開如下字段: ? x ? ? The x component of the point. ? y ? ? The y component of the point.
? CGSize公開如下字段: ? width ? ? The width component of the size. ? height ? ? The height component of the size.
? CGRect公開如下字段: ? 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的屬性方法來指定一個結(jié)構(gòu)字段的關鍵路徑。如下的代碼是無法正常執(zhí)行的: ? myLayer.transform.rotation.x=0;
? 相反你必須使用setValue:forKeyPath:或者valuForKeyPath:,如下: ? [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
轉(zhuǎn)自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-10-kvc.html
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協(xié)議,會自動歸檔這些你設置的額外鍵。
1.2 支持默認值
? 核心動畫添加的鍵值編碼約定,允許一個類在被使用時鍵沒有被設置相應值的時候提供默認值。CALayer或CAAnimation支持該約定,通過使用方法defaultValueForKey:。
? 為了給鍵提供默認值,你創(chuàng)建相應的子類,并重載defaultValueForKey:。子類實現(xiàn)相應的鍵參數(shù)檢查并返回適當?shù)哪J值。清單1描述了一個實現(xiàn)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)時候,你可以使用如下的封裝約定: ? CGPoint ? ? NSValue ? CGSize ? ? NSValue ? CGRect ? ? NSValue ? CGAffineTransform ? ? NSAffineTransform (Mac OS X only) ? CATransform3D ? ? NSValue
1.4 支持結(jié)構(gòu)字段的關鍵路徑
? CAAnimation提供支持使用關鍵路徑訪問選擇的結(jié)構(gòu)字段。這在為動畫關鍵路徑指定結(jié)構(gòu)字段的時候非常有幫助,同時你可以使用setValue:forKeyPath:和valueForKeyPath來設置和讀取相應的值。
? CATransform3D公開如下的字段: ? 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公開如下字段: ? x ? ? The x component of the point. ? y ? ? The y component of the point.
? CGSize公開如下字段: ? width ? ? The width component of the size. ? height ? ? The height component of the size.
? CGRect公開如下字段: ? 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的屬性方法來指定一個結(jié)構(gòu)字段的關鍵路徑。如下的代碼是無法正常執(zhí)行的: ? myLayer.transform.rotation.x=0;
? 相反你必須使用setValue:forKeyPath:或者valuForKeyPath:,如下: ? [myLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"transform.rotation.x"];
轉(zhuǎn)自夢維:http://www.dreamingwish.com/dream-2012/coreanimation-programming-guide-10-kvc.html
總結(jié)
以上是生活随笔為你收集整理的CoreAnimation编程指南(十)KVC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [设计模式] ------ 模板模式
- 下一篇: 5gh掌上云计算认证不通过_阿里云ACP