views:

73

answers:

3

I want my text box to blink (like the old LCD clocks) and 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 just feels like hax, you know.

A: 

NSTimers and changing the alpha is a perfectly acceptable way of doing it - that's certainly what I do. If you are having problems, perhaps a code sample might help us see where the issue is?

Roger
There aren't any problems, it just looks a little flaky. I'm a graphic artist, and so I don't want my applications to look like the so many others on the App Store.
SeniorShizzle
The trouble is, without being able to see your code, it's hard to figure out where the "flakyness" is coming from. It's most likely to be something unrelated to the use of NSTimer per se, are you just trying to flash a single text box on screen? What sort of speed? What else are you doing on the main UI thread? How many other layers are in the view heirarchy etc etc, there are lots of possible considerations.
Roger
+3  A: 

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.

Taylan Pince
That's a lot better. Thanks! Is there any way to make the animation faster, though?
SeniorShizzle
Updated answer with reference to duration and iOS4.
Taylan Pince
You can change the duration;[UIView setAnimationDuration:0.1f];You may want to consider the profile of the animation - see the documentation for more details Eg. [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
Roger
Thanks. Actually i feel stupid now. I realized that about 2 minutes after I posted the comment.
SeniorShizzle
+1  A: 

I would use an NSTimer, but instead of messing with alpha channels i would either not draw the text (if that's even possible with Apple's very attribute-limited SDK), or if that's not possible you could always draw something on top of it (like a rectangle).

Using this approach of drawing something over your text would yield better performance. Though some (okay most) would consider this a ugly hack, let me just say this, "If it looks right, it is right."

AlvinfromDiaspar
Yet another hack would be placing the position of the text somewhere way off the screen. This way you dont have to draw anything over it AND you dont have to worry about figuring out how to hide the text. Note: if you could toggle the visibility of a control that would be great. if you figure that out please let me know! :)
AlvinfromDiaspar
@Alvin Toggle the visibility? Use the [hidden](http://developer.apple.com/iphone/library/iPad/index.html#documentation/uikit/reference/UIView_Class/UIView/UIView.html) property.
progrmr