SeniorShizzle SeniorShizzle - 6 months ago 21
Objective-C Question

Best method to get text to blink in iPhone OS?

I want my text box to blink (like the old LCD clocks).

Right now, I'm calling a myriad of NSTimers and selectors that wait, change the alpha, wait, then change it back. Even with this, it looks really bad, and I'm thinking I have to put an NSTimer to gradually change the alpha, but from what I hear they are not meant for things of that precision.

My thoughts are there must be a way to do this a lot better than how I am currently implementing it. It feels like hack.

Answer

Using an animation delegate might make it less "hacky":

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[myLabel setAlpha:0.0];
[UIView commitAnimations];

And then you can have your didStopSelector restart the animation:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [self displayLabel];
}

Depending on the animationID, you could take different actions, etc. Using UIView's setAnimationDelay might come in handy as well.

UIView also has a setDuration call for animations:

[UIView setAnimationDuration:0.1];

If you are building for iOS4, check the documentation since you should be using block-based animation calls rather than these delegate based ones.