Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I fade / animate the tintColor of a UIToolbar?

I am trying to animate a UIToolbar's tintColor property, to change it from one tintColor to another.

Here is the code I am trying. Unfortunately, the change occurs immediately and does not fade from green to blue. This is strange because I know Apple fades and "pulses" toolbar tint colors when tethering or on a phone call. So why doesn't this work?

// set initial tint color  
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.95 blue:0.15 alpha:0.6];

//animation stuff  
[UIView beginAnimations:nil context:nil];  
[UIView setAnimationDuration:1.95];  
[UIView setAnimationDelegate:self];        

//thing to animate  
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.35 blue:0.45 alpha:0.6];

//animation stuff  
[UIView commitAnimations]; 
like image 420
Bryce Avatar asked Dec 20 '10 23:12

Bryce


2 Answers

The tint color in not animatable through public APIs. You can work around this by manually changing the tint color on a timer. You would have to interpolate the intermediate color levels.

like image 98
logancautrell Avatar answered Nov 18 '22 05:11

logancautrell


Each UIView has a tintAdjustmentMode:

The first non-default tint adjustment mode value in the view’s hierarchy, ascending from and starting with the view itself.

When this property’s value is dimmed, the value of the tintColor property is modified to provide a dimmed appearance.

For example, if you need to dim the current tintColor (dim = grey color) just use the code below:

// Assuming tintAdjustmentMode is set to .automatic
UIView.animate(withDuration: 0.5) {
   yourView.tintAdjustmentMode = .dimmed
}

This will animate the tintColor to grey and will dim it.

If you want to animate the tintColor to your own custom color:

// Make sure it is set to normal and not automatic
yourView.tintAdjustmentMode = .normal

UIView.animate(withDuration: 0.5) {
   yourView.tintColor = .black
}
like image 2
OhadM Avatar answered Nov 18 '22 05:11

OhadM