CALayer子类不动画属性更改

时间:2013-08-17 12:49:30

标签: ios calayer caanimation

我有一个CALayer子类,其浮动animAngle属性标记为@dynamic。我为子类实现了方法actionForKeyinitWithLayerneedsDisplayForKeydrawInContextactionForKey的定义如下

- (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”属性。 我在这里可能做错了什么?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

CocoaHeads Session: Rob Napier on Animating Custom Layer Properties是关于自定义动画的精彩演示。

CALayers讨厌画画;)