CALayer subclass not animating to property changes
Asked Answered
R

2

6

I have a CALayer subclass with float animAngle as property marked as @dynamic. I have implemented methods actionForKey, initWithLayer, needsDisplayForKey and drawInContext for subclass. The definition for actionForKey is as follows

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

And

- (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;
}

In Someother class:

myCASublayer.animAngle = 0.5f;

Somehow the CABasicAnimation being returned is not able to properly use the layer "animAngle" property. What would i be possibly doing wrong here?

Rustle answered 17/8, 2013 at 12:49 Comment(5)
One is called animAngle the other is wiggleAngleDisk
@RobvanderVeer: Sorry that was a typo. Its animAngle.Rustle
@basstrekerk87, better copy/paste next time, saves errors! You know you don't actually have to indent code manually? Use the button or start with 3 backquotes (`)Disk
@RobvanderVeer: Did use add code button, still misaligned the code somehow. will get better at itRustle
i still am banging my head around this. can somebody help here!!!Rustle
S
1

CocoaHeads Session: Rob Napier on Animating Custom Layer Properties is a good presentation about custom animations.

CALayers hate do draw ;)

Sneakbox answered 21/8, 2013 at 14:30 Comment(2)
thanks, it works. but if the animation is infinite in repeat count, it gives only animates once. Is drawInContext not called for repeated animations?Rustle
@basstrekker87 I'm not sure if action for key can support repeating. AFAIK it's more or less an implicit animation from an old value to a new value.Allsopp
C
1

If animAngle is a @property - you must specify accessors for this property.
When you mark property as @dynamic this means, that you will provide an implementation of those methods dynamically at runtime. So, if you do not provide accessors for property, you can't access it.

Craquelure answered 17/8, 2013 at 19:20 Comment(4)
tried creating accessors for animAngle but still not working. @dynamic is required for CALayer subclasses which provides implementations for the property itself, but anyhows changes are still not getting updatedRustle
That is not really true for CALayers. They are ... "special" ;)Allsopp
@DavidRönnqvist: Whats untrue. the answer by Lexandr or the comment i added.Rustle
@basstrekker87 The code was targeted at the answer, otherwise I would have used @ reply like you did and like this comment does. Layers generate their own accessors and can have any object set using setValue:forKey even though it may not even be a property on CALayer.Allsopp
S
1

CocoaHeads Session: Rob Napier on Animating Custom Layer Properties is a good presentation about custom animations.

CALayers hate do draw ;)

Sneakbox answered 21/8, 2013 at 14:30 Comment(2)
thanks, it works. but if the animation is infinite in repeat count, it gives only animates once. Is drawInContext not called for repeated animations?Rustle
@basstrekker87 I'm not sure if action for key can support repeating. AFAIK it's more or less an implicit animation from an old value to a new value.Allsopp

© 2022 - 2024 — McMap. All rights reserved.