CATransaction 未观察到 CALayer 子类上的属性
我有一个带有自定义属性的 CALayer
子类,声明如下:
@interface MyLayer : CALayer
@property (nonatomic,retain) NSNumber *customValue;
@end
@implementation MyLayer
@synthesize customValue = _customValue;
@end
我想在显式 CATranasction
内为该属性设置动画,因此我使用 < code>actionForLayer:forKey: 实现的方法返回动画,但是对 [CATransaction begin] ... [CATransaction end] 内的
someMyLayerInstance.customValue
进行任何更改>不会导致 actionForLayer:forKey
被相应的“key”值调用。
但是,通过调用 setValue:forKey:
取消 MyLayer
上的属性并更改 myLayerInstance
会导致 < code>actionForLayer:forKey: 被调用。
看来这是因为 CALayer 对未定义属性的键/值编码做了一些魔力。如何重新创建这个魔力,以便我可以在 MyLayer
上声明属性,但仍然让动画委托观察它们?
I have a subclass of CALayer
with a custom property, declared as such:
@interface MyLayer : CALayer
@property (nonatomic,retain) NSNumber *customValue;
@end
@implementation MyLayer
@synthesize customValue = _customValue;
@end
I want to animate this property inside of an explicit CATranasction
, so i set up a delegate with the actionForLayer:forKey:
method implemented which returns an animation, however any changes to someMyLayerInstance.customValue
inside of [CATransaction begin] ... [CATransaction end]
do not result in actionForLayer:forKey
getting called with a corresponding 'key' value.
However, nuking the property on MyLayer
and making changes to myLayerInstance
by calling setValue:forKey:
does result in actionForLayer:forKey:
getting called.
It appears that this is because CALayer
does some mojo for key/value coding for undefined properties. How can I recreate this mojo so that I can declare properties on MyLayer
, but still have them be observed by the animation delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最重要的是,您需要使用
@dynamic
实现所有CALayer
访问器。不要使用@synthesize
并且不要直接实现访问器。 CALayer 生成所有自己的属性处理程序(正如您间接发现的那样),您需要让它们被使用。您还需要让 CALayer 知道此属性会影响显示(根据您的其他评论,您可能已经这样做了)。如果还没有,您可以通过实现
+needsDisplayForKey:
并为您的密钥返回YES
来完成此操作。下面是一个为自定义属性设置动画的 CALayer 示例(取自 iOS 5 编程突破极限的第 7 章完整的示例代码可在 Wiley 站点上找到。)此示例在层中实现了
actionForKey:
,但是你仍然可以实现该部分如果您愿意,请代表。The most important thing is that you need to implement all
CALayer
accessors using@dynamic
. Do not use@synthesize
and do not implement the accessors directly.CALayer
generates all its own property handlers (as you've indirectly discovered), and you need to let those be used.You also need to let
CALayer
know that this property is display-impacting (which you may have already done given your other comments). If you haven't, you do this by implementing+needsDisplayForKey:
and returningYES
for your key.Here's an example of a
CALayer
that animates a custom property (taken from Chapter 7 of iOS 5 Programming Pushing the Limits. The full sample code is available at the Wiley site.) This example implementsactionForKey:
in the layer, but you can still implement that part in the delegate if you prefer.