CALayer opacity animation

The best way to accomplish this is to use an explicit animation (see guide) by creating an instance of CABasicAnimation and adding it to the layer.

The code would look something like this:

CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
flash.fromValue = [NSNumber numberWithFloat:0.0];
flash.toValue = [NSNumber numberWithFloat:1.0];
flash.duration = 1.0;        // 1 second
flash.autoreverses = YES;    // Back
flash.repeatCount = 3;       // Or whatever

[layer addAnimation:flash forKey:@"flashAnimation"];

If you want to know when the animation is done you can set a delegate and implement the animationDidStop:finished: method, however it's best to use a completion block as that allows all the code to be in the same place. If you are writing for iOS 4 or OS X then you can use the excellent CAAnimationBlocks category to accomplish this.


Trojanfoe's answer is excellent. I just want to add that if you want more control over the "timeline" (how long should it take to fade out? how long should we then wait? then how long should it take to fade in? and so on) you're going to want to combine multiple CABasicAnimations into a CAAnimationGroup.

You might want to read my book chapter on this topic, the last part of which constitutes a tutorial on CAAnimation and its offspring:

http://www.apeth.com/iOSBook/ch17.html#_core_animation

Note that my discussion is directed at iOS; on Mac OS X, if that's where you are, the view/layer architecture is a little different, but what it says about CAAnimation is still correct.