El Tomato
El Tomato

Reputation: 6707

CAKeyframeAnimation - Stopping Infinite Loop

I have the following lines of code to run animation with some frames.

- (void)setAnimation {
    /* creating a layer */
    CALayer *layer = [[CALayer alloc] init];
    [layer setBounds:self.bounds];
    [layer setPosition:NSMakePoint(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds))];
    [self.layer addSublayer:layer];

    /* creating frames */
    NSArray *array = [appDelegate getAnimationFrames]; // Extracting an array containing NSImage objects from appDelegate

    /* setting up animation */
    keyFrameAnimation = [CAKeyframeAnimation animation]; // A CAKeyframeAnimation object declared in the header file
    [keyFrameAnimation setKeyPath:@"contents"];
    [keyFrameAnimation setValues:array];
    [keyFrameAnimation setCalculationMode:@"discrete"];
    [keyFrameAnimation setRemovedOnCompletion:NO];
    [keyFrameAnimation setRepeatCount:HUGE_VALF];
    keyFrameAnimation.duration = 1.0;
    [layer addAnimation:keyFrameAnimation forKey:nil];
}

This is my first time using CAKeyframeAnimation. And it works fine for now. The only question I have is how to break the loop if the repeat count is set to HUGE_VALF. The API documentation doesn't appear helpful in this regard since it will return nothing if I search for 'setRepeatCount.' Meanwhile, the code is for a Cocoa application, but I'll open this topic for iOS as well since there is little departure between OS X and iOS as far as this topic is concerned.

Thanks.

Upvotes: 3

Views: 815

Answers (1)

Duncan C
Duncan C

Reputation: 131418

Once the animation is added to the layer the layer creates a copy, so further changes to the animation object don't do anything.

You can use [layer removeAllAnimations] to stop the animation.

Upvotes: 3

Related Questions