CALayer和CAAnimation對未實現屬性訪問器的動態解析 (CALayer and CAAnimation's dynamic resolution of unimplemented property accessors)


問題描述

CALayer和CAAnimation對未實現屬性訪問器的動態解析 (CALayer and CAAnimation's dynamic resolution of unimplemented property accessors)

I found that CALayer and CAAnimation don't only extend the NSKeyValueCoding protocol as described in Core Animation Extensions To Key-Value Coding, but also offer dynamic resolution for unimplemented property accessors. For example:

@interface DotLayer : CALayer
@property (nonatomic, retain) id dot;
@end

@implementation DotLayer
@dynamic dot;
@end

Simply with a property declaration and stating it is @dynamic, I can access dot property without implementing its accessors:

DotLayer *layer = [DotLayer layer];
NSLog(@"layer responds to dot: %d", [layer respondsToSelector:@selector(dot)]);
layer.dot = nil;
NSLog(@"%@", [layer dot]);

After further investigation, I found this dynamic resolution is done by CALayer and CAAnimation's special implementation of +resolveInstanceMethod:.

I saw usage of this dynamic resolution in ImageBrowser sample code of WWDC 2010, but I can not find any documentation stating this feature. So I'm wondering: Is this dynamic resolution a prescribed behavior that I can make use of in my own code?


參考解法

方法 1:

After further thought, I have my own guess: To extend the NSKeyValueCoding protocol, CALayer and CAAnimation offer dynamic resolution for all unimplemented property accessors in their implementation of +resolveInstanceMethod:. As a side effect, @dynamic properties without accessors are also covered.

I'm not sure whether the WWDC sample code is written this way intentionally or accidentally. But if my guess is correct, it is quite implementation dependent so I think we should not utilize it as a feature.

(by an0an0)

參考文件

  1. CALayer and CAAnimation's dynamic resolution of unimplemented property accessors (CC BY-SA 3.0/4.0)

#core-animation #iOS






相關問題

CATextLayer 中不需要的填充 (Unwanted padding in CATextLayer)

NSTableColumn 動畫 (NSTableColumn animation)

自定義視圖控制器上的“編譯器錯誤”、“分段錯誤 11” ("Compiler Bug", "Segmentation Fault 11" On Custom View Controller)

在情節提要中為“平鋪”創建更大的反面視圖 (Creating a larger flip-side view for 'tile' in storyboard)

UIView 組動畫 (UIView group animation)

分組表視圖中的中間動畫看起來很糟糕 (Middle animation in grouped table view looks horrible)

Làm thế nào để triển khai hoạt ảnh nhấp nháy trong iOS? (How to implement blink animation in iOS?)

iphone cocoa:如何沿路徑拖動圖像 (iphone cocoa : how to drag an image along a path)

圖層有什麼用?添加圖層可以做什麼,為什麼要考慮圖層? (What are Layers good for? What could I do with adding an Layer, and why should I think about Layers?)

CALayer和CAAnimation對未實現屬性訪問器的動態解析 (CALayer and CAAnimation's dynamic resolution of unimplemented property accessors)

使用 Core Animation/CALayers 為時鐘設置動畫 (Animating a Clock with Core Animation/CALayers)

在 UIView 和 subiview 的 CALayer 上結合動畫? (Combining animations on UIView and subiview's CALayer?)







留言討論