In my iOS 5 app I have a custom UIButton
that has a red orb as its image. When the button is tapped I would like the orb to start pulsing/fading between red and green. I have both red and green images and can successfully cross-disolve between the two using the following code:
CABasicAnimation *imageSwitchAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
imageSwitchAnimation.fromValue = (id)[UIImage imageNamed:@"red.png"].CGImage;
imageSwitchAnimation.toValue = (id)[UIImage imageNamed:@"green.png"].CGImage;
imageSwitchAnimation.duration = 1.5f;
[self.button.imageView.layer addAnimation:imageSwitchAnimation forKey:@"animateContents"];
However, I would like the animation to continue forever (well, until I tell it to stop) and also for the animation to reverse and loop. In other words, fade red -> green -> red and then repeat.
I tried putting the animation block above into an endless loop (together with some logic to determine whether the fading should go from red -> green or green -> red) but this just locks-up the entire app.
Other solutions seem to use Cocos2d, which seems fairly heavy-weight since this is the only animation I need in the app (so I don't want to use such frameworks unless absolutely needed).
Any help would be greatly appreciated.