Chaining animations and memory management

Posted by bryan1967 on Stack Overflow See other posts from Stack Overflow or by bryan1967
Published on 2009-09-22T21:16:03Z Indexed on 2010/06/13 3:02 UTC
Read the original article Hit count: 328

Hey Everyone,

Got a question. I have a subclassed UIView that is acting as my background where I am scrolling the ground. The code is working really nice and according to the Instrumentation, I am not leaking nor is my created and still living Object allocation growing.

I have discovered else where in my application that adding an animation to a UIImageView that is owned by my subclassed UIView seems to bump up my retain count and removing all animations when I am done drops it back down.

My question is this, when you add an animation to a layer with a key, I am assuming that if there is already a used animation in that entry position in the backing dictionary that it is released and goes into the autorelease pool?

For example:


- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    NSString *keyValue = [theAnimation valueForKey:@"name"];
    if ( [keyValue isEqual:@"step1"] && flag ) {
    	groundImageView2.layer.position = endPos;

    	CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:@"position"];
    	position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    	position.toValue = [NSValue valueWithCGPoint:midEndPos];
    	position.duration = (kGroundSpeed/3.8);
    	position.fillMode = kCAFillModeForwards;
    	[position setDelegate:self];
    	[position setRemovedOnCompletion:NO];
    	[position setValue:@"step2-1" forKey:@"name"];

    	[groundImageView2.layer addAnimation:position forKey:@"positionAnimation"];	

    	groundImageView1.layer.position = startPos;
    	position = [CABasicAnimation animationWithKeyPath:@"position"];
    	position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    	position.toValue = [NSValue valueWithCGPoint:midStartPos];
    	position.duration = (kGroundSpeed/3.8);
    	position.fillMode = kCAFillModeForwards;
    	[position setDelegate:self];
    	[position setRemovedOnCompletion:NO];
    	[position setValue:@"step2-2" forKey:@"name"];

    	[groundImageView1.layer addAnimation:position forKey:@"positionAnimation"];	
    }
    else if ( [keyValue isEqual:@"step2-2"] && flag ) {
    	groundImageView1.layer.position = midStartPos;

    	CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:@"position"];
    	position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    	position.toValue = [NSValue valueWithCGPoint:endPos];
    	position.duration = 12;
    	position.fillMode = kCAFillModeForwards;
    	[position setDelegate:self];
    	[position setRemovedOnCompletion:NO];
    	[position setValue:@"step1" forKey:@"name"];

    	[groundImageView1.layer addAnimation:position forKey:@"positionAnimation"];	
    }
}

This chains animations infinitely, and as I said one it is running the created and living object allocation doesn't change. I am assuming everytime I add an animation the one that exists in that key position is released.

Just wondering I am correct. Also, I am relatively new to Core Animation. I tried to play around with re-using the animations but got a little impatient. Is it possible to reuse animations?

Thanks! Bryan

© Stack Overflow or respective owner

Related posts about iphone

Related posts about memory-management