Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer subclass not animating to property changes

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?

like image 252
Madhur Rawat Avatar asked Aug 17 '13 12:08

Madhur Rawat


2 Answers

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

CALayers hate do draw ;)

like image 97
David Rönnqvist Avatar answered Oct 10 '22 09:10

David Rönnqvist


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.

like image 22
Lexandr Avatar answered Oct 10 '22 10:10

Lexandr