CATransaction 未观察到 CALayer 子类上的属性

发布于 2024-12-28 11:39:43 字数 769 浏览 0 评论 0原文

我有一个带有自定义属性的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2025-01-04 11:39:43

最重要的是,您需要使用 @dynamic 实现所有 CALayer 访问器。不要使用@synthesize并且不要直接实现访问器。 CALayer 生成所有自己的属性处理程序(正如您间接发现的那样),您需要让它们被使用。

您还需要让 CALayer 知道此属性会影响显示(根据您的其他评论,您可能已经这样做了)。如果还没有,您可以通过实现 +needsDisplayForKey: 并为您的密钥返回 YES 来完成此操作。

下面是一个为自定义属性设置动画的 CALayer 示例(取自 iOS 5 编程突破极限的第 7 章完整的示例代码可在 Wiley 站点上找到。)此示例在层中实现了 actionForKey:,但是你仍然可以实现该部分如果您愿意,请代表。

@implementation CircleLayer
@dynamic radius;

...

+ (BOOL)needsDisplayForKey:(NSString *)key {
  if ([key isEqualToString:@"radius"]) {
    return YES;
  }
  return [super needsDisplayForKey:key];
}

- (id < CAAction >)actionForKey:(NSString *)key {
  if ([self presentationLayer] != nil) {
    if ([key isEqualToString:@"radius"]) {
      CABasicAnimation *anim = [CABasicAnimation
                                animationWithKeyPath:@"radius"];
      anim.fromValue = [[self presentationLayer] 
                        valueForKey:@"radius"];
      return anim;
    }
  }

  return [super actionForKey:key];
}

@end

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 returning YES 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 implements actionForKey: in the layer, but you can still implement that part in the delegate if you prefer.

@implementation CircleLayer
@dynamic radius;

...

+ (BOOL)needsDisplayForKey:(NSString *)key {
  if ([key isEqualToString:@"radius"]) {
    return YES;
  }
  return [super needsDisplayForKey:key];
}

- (id < CAAction >)actionForKey:(NSString *)key {
  if ([self presentationLayer] != nil) {
    if ([key isEqualToString:@"radius"]) {
      CABasicAnimation *anim = [CABasicAnimation
                                animationWithKeyPath:@"radius"];
      anim.fromValue = [[self presentationLayer] 
                        valueForKey:@"radius"];
      return anim;
    }
  }

  return [super actionForKey:key];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文