Briefly change colors in a UILabel - ios

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];

Related

navigationBar change titleView with animation

I would like to change the content of my titleView in the navigationBar to display different kinds of information, for instance a UIsegmentedControl or a UILabel.
The change of the information should be a done with a transition (segmentedControl fades out and label fades in).
I tried to add multiple UIViews in the titleView and animate the alpha value, but it's not working.
[self.navigationItem.titleView addSubview:_mainSegmentedControl];
So I tried to change the content during the animation but since the value can't be animated it will be called immediately:
[UIView animateWithDuration:0.2 animations:^{
_filterSegmentedControl.alpha = 0.0f;} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
self.navigationItem.titleView = _mainSegmentedControl;
_mainSegmentedControl.alpha = 1.0f;
}];
}];
Any suggestions?

Updating view background color with animation makes app unresponsive

I encountered the problem, can't find a good solution so far.
After some certain actions I want to change the background color of my self.view with animation. So I do something like this:
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.view.backgroundColor = [UIColor orangeColor];
} completion:^(BOOL finished) {
self.backgroundColor = self.view.backgroundColor;
}];
And I want this animation to be slow and easy, that's why it can be likfe 3-5 seconds, not just a quick switch form one to another.
The problem is that if I make it on the Main thread it makes my app completely unresponsive for these 3-5 seconds while the animation is performing. If I make it on the background thread I have a lot of problem, which is expected.
Is there any good solution how to slowly update background color with animation without freezing the screen?
Thanks!
You need to use UIViewAnimationOptionAllowUserInteraction option instead of UIViewAnimationOptionCurveEaseOut to allow user interaction.
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.view.backgroundColor = [UIColor orangeColor];
} completion:^(BOOL finished) {
self.backgroundColor = self.view.backgroundColor;
}];

UIView animationWithDuration not working second when called twice

I have created a category on UITextField which allows to animate between red and white color as shown below:
#implementation UITextField (Additions)
-(void) flash
{
[UIView animateWithDuration:2.0 animations:^{
[self setBackgroundColor:[UIColor redColor]];
} completion:^(BOOL finished) {
[self setBackgroundColor:[UIColor whiteColor]];
}];
}
#end
I call it like this:
[self.paymentTextField flash];
First time it works and it shows the red background and then switches back to white color. But on second call it does not do anything.
I'm not sure why that doesn't work. I tried it and got the same thing. However, since the color doesn't really animate, you can do it like this instead:
self.backgroundColor = [UIColor redColor];
[self performSelector:#selector(setBackgroundColor:) withObject:[UIColor whiteColor] afterDelay:2];
This should give you the same look.

Animate View Background Changes

I have a view I change the background of as the state of the application changes. Basically I want the view background changes to animate in some sort of a slide. I've been looking for a solution, but haven't really found anything.
So I want to have a transition from whatever the current background is to the next one.
//current to:
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
I have no trouble getting the background to change but I would like to animate it. Any help in the right direction would be greatly appreciated thanks.
You can use this:
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^
{
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
completion:nil
];
a very easy way to handle it:
such as yourView.backgroundColor = [UIColor redColor];
Then use this to animate change the color
#import <QuartzCore/QuartzCore.h>
yourView.backgroundColor = [UIColor blueColor];
CATransition *transiton = [[CATransition alloc] init];
transiton.duration = .5;
[yourView.layer addAnimation:transiton forKey:#"Animation"];
Just a simple animation block:
[UIView animateWithDuration:time animations^{
self.view.backgroundColor = [UIColorWithPatternImage:image];
}];
this just animates the transfer, taking time seconds.

Animating UITableViewCell's backgroundColor in block animation

I'm trying to animate two table cells background colors from red back to the original color, white.
The following code is what I'm using. The problem is that it never shows the red color -- it simply animates from (original) white to (animated-to) white. I.e., if I change the color in the animation block, it will animate to that color.
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].backgroundColor = [UIColor redColor];
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]].backgroundColor = [UIColor redColor];
[UIView animateWithDuration:0.8 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].backgroundColor = [UIColor whiteColor];
[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]].backgroundColor = [UIColor whiteColor];
}
completion:^(BOOL finished) {
}];
The following code works as expected for the table's background, but that's not what I want:
table.backgroundColor = [UIColor redColor];
[UIView animateWithDuration:0.8 delay:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
table.backgroundColor = [UIColor clearColor];
}
completion:^(BOOL finished) {
}];
So, why doesn't my cell background animate when my table background does?
For what it's worth, I have a bunch of other chained animations I perform on the table view right after this, but having commented those animations out, I still have this issue.
Because a cell contains other views. You have to check the color of cell.contentView, cell.accessoryView and cell.backgroundView. If all of these are [UIColor clearColor], you can just animate cell.backgroundColor.
It looks like #Jason Coco is right. I can't do it (cleanly and quickly):
From Apple:
Note: If you want to change the background color of a cell (by setting
the background color of a cell via the backgroundColor property
declared by UIView) you must do it in the
tableView:willDisplayCell:forRowAtIndexPath: method of the delegate
and not in tableView:cellForRowAtIndexPath: of the data source.
Changes to the background colors of cells in a group-style table view
has an effect in iOS 3.0 that is different than previous versions of
the operating system. It now affects the area inside the rounded
rectangle instead of the area outside of it.
Reference
So if I really wanted to I'd have to set some variable, then call reloadData on the table or something similar.

Resources