Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I chain scale animations with an iPhone UIImageView?

I'm trying to scale an image down, change the image, then scale it back up.

CABasicAnimation* shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrink.toValue = [NSNumber numberWithDouble:0];
shrink.duration = 1;
    shrink.delegate = self;
    [myImageView.layer addAnimation:shrink forKey:@"shrink"];   

makes the shrink, then when it completes, I change the image, and start the grow:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{
    myImageView.image = [images objectAtIndex:image];
CABasicAnimation* grow = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
grow.toValue = CGAffineTransformMakeScale(1,1);
    grow.delegate = self;
grow.duration = 1;
[myImageView.layer addAnimation:grow forKey:@"grow"];   
}

This works great on the simulator, but on the device, when the shrink completes, I get a flash of the full-size, old image, then the grow animation begins with the new image.

Any idea how to get rid of that flash?

(I've tried "removedOnCompletion = NO;" and attempted setting the affineTransform equal to the scaled down size after the first completion, but didn't have much luck.)

Any tips appreciated.

kb

Edit:

Excellent! Setting the following:

shrink.fillMode = kCAFillModeForwards; 
shrink.removedOnCompletion = NO; 

Removed the flashing. Thanks, Ben!

like image 906
KevinButler Avatar asked Mar 06 '09 07:03

KevinButler


1 Answers

Try setting your animation's fillMode to kCAFillModeForwards. That should leave the item as it was at the end of the animation, rather than before.

like image 132
Ben Gottlieb Avatar answered Oct 06 '22 00:10

Ben Gottlieb