How reliable [CALayer animationForKey:] is?

697 Views Asked by At

I have a subclass of CALayer called MyLayer:

@interface MyLayer : CALayer
@property (nonatomic,readonly) BOOL busy;
-(void)performTask1;
-(void)performTask2;
-(void)performTask3;
@end

In performTask* functions I say:

-(void)performTask*
{
    CAKeyframeAnimation *animation = [...];
    ...
    animation.removedOnCompletion = YES; // "YES" is default anyway
    ...
    [self addAnimation:animation
                forKey:TASK*_KEY];
}

The busy property is for the caller and implemented like this:

@dynamic busy;
-(BOOL)busy
{
        // i.e. for some specific ids
    return ([self animationForKey:TASK1_KEY] != nil ||
            [self animationForKey:TASK3_KEY] != nil);
}

What I see is that this approach is not reliable... I can see on the screen that animation is finished (nothing is moving, etc.), while animationForKey: does NOT return nil. The behavior is semi-random... Most of times it goes as expected. But sometimes it takes 1-2 seconds before I start getting nil-s.

This weird behavior disappears, if I set a delegate to animation animation.delegate = self and implement animationDidStop:finished:.

Someone experienced this as well?

1

There are 1 best solutions below

0
On

Declaring @dynamic causes CALayer to implement the accessor method for you (see Properties on CALayer subclass aren't getting observed by CATransaction and CALayer and CAAnimation's dynamic resolution of unimplemented property accessors). So it's possible that you are just seeing confusion in which version of "busy" is getting called. Why did you declare "@dynamic" in the first place?

There could also be something funky happening with animationForKey, but I'd try removing the "@dynamic" first.