6

我有一个CALayer子类,其中 floatanimAngle作为属性标记为@dynamic. 我已经实现了方法actionForKey、、initWithLayerneedsDisplayForKeydrawInContext类。的定义actionForKey如下

- (id<CAAction>)actionForKey:(NString *)event {
    if([event isEqualToString:@"animAngle"]) {
        return [self animationForKey:event];
    }
    return [super actionForKey:event];
}

- (CABasicAnimation *)animationForKey:(NSString *)key
{
    NSString *animValue = [[self presentationLayer] valueForKey:key];// Logs as 0
    CABasicAnimation *anim;

    if([key isEqualToString:@"animAngle"]) {
        anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        anim.repeatCount = HUGE_VAL;
        anim.autoreverses = YES;
        //anim.fromValue = [[self presentationLayer] valueForKey:key]; // setting animation value from layer property as in here does not work.
        anim.fromValue = [NSNumber numberWithFloat:0.5f];            // This works
    }
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anim.duration = 0.11;
    return anim;
}

在其他类:

myCASublayer.animAngle = 0.5f;

不知何故,CABasicAnimation被返回的东西无法正确使用图层“ animAngle”属性。我在这里可能做错了什么?

4

2 回答 2

1

如果animAngle@property- 您必须accessors为此属性指定。
当您将属性标记为@dynamic这意味着您将在运行时动态地提供这些方法的实现。因此,如果您不为属性提供访问器,则无法访问它。

于 2013-08-17T19:20:11.660 回答
1

CocoaHeads Session: Rob Napier on Animating Custom Layer Properties是关于自定义动画的一个很好的演示。

CALayers 讨厌画画 ;)

于 2013-08-21T14:30:22.640 回答