I have a label that gets updated with values calculated from information received by the server. Depending on how much traffic there is the updates can come in rapidly or at random intervals. What I would like to do is compare the new calculated value with the old value and if it has increased then change the text to be green and the background to be a darker green and if the value has decreased then change the text to a shade of red and dark red.
That part is easy to do, what I am having some trouble is after a half a second or so I would like to change the background and text to be their default colors. I'm doing this as part of user feedback for when the values change.
Any information would be greatly appreciated.
A label is a subclass of UIView. Assuming you have some method that notifies you of when the change occurs...
- (void)someMethodThatNotifiesOfChange {
// Calculate new values and assume the result means color button green
[UIView animateWithDuration:0.5 animations:^{
label.backgroundColor = [UIColor greenColor];
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.5 delay:5.0 options:nil animations:^{
label.backgroundColor = [UIColor clearColor];
} completion:nil];
}];
}
This just changes the background to be green, but illustrates the point none-the-less. This take 0.5 seconds to animate to green. Then, when it is complete, it waits 5 seconds and then takes 0.5 to animate back to clear.
You can use CoreAnimation library if you want advanced animation but if you just animate UILabel properties, you can use UIView static method to do this. You can do it like this:
[UIView animateWithDuration:0.3f animations:^{
label.textColor = [UIColor greenColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3f animations:^{
label.textColor = [UIColor blackColor];
} completion:nil];
}];
In first animation block, you set label's text color (and background color if you want). In its completion block you set the label's color back using another animation.
Hope it helps.
Take a look at NSTimer. Timers are useful to lots of situations!
Good luck!
Alternatively by IOS 4 the followwing block would do the same effect
[UIView transitionWithView: infoLabelInternet duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
infoLabelPremium.textColor = kPopoverActiveTextColor;
} completion:nil];
You can also do it this way:
label.backgroundColor = [UIColor greenColor];
[label performSelector:@selector(setBackgroundColor:) withObject:[UIColor clearColor] afterDelay:0.5];