Reputation: 249
Is there a way to animate the textColor property of a UIButton? I've found some solutions for UILabel text color animation, but haven't seen any for UIButton.
Upvotes: 12
Views: 5927
Reputation: 36
This is answered very well here. It basically uses this handy guy :
[UIView transitionWithView:duration:options:animations:^()completion:^()];
Upvotes: 1
Reputation: 9410
You can use two buttons (or labels, etc) with different colors which placed at same position:
button1 = UIButton(frame: frame)
button2 = UIButton(frame: frame) // same frame
button1.setTitleColor(UIColor.redColor(), forState: .Normal) // red
button2.setTitleColor(UIColor.blueColor(), forState: .Normal) // blue
After that you can reach animation of colors by transition animation of button2:
UIView.animateKeyframesWithDuration(
1.0, delay: 0, options: [.Autoreverse, .Repeat],
animations: {
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.5, animations: {
self.button2.alpha = 1.0
})
UIView.addKeyframeWithRelativeStartTime(0.5, relativeDuration: 0.5, animations: {
self.button2.alpha = 0.0
})
}, completion: nil)
Upvotes: 0
Reputation: 4196
Use transition with view
[UIView transitionWithView:backButton
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[backButton setTitleColor:[_peacock lighten:d.primary] forState:UIControlStateNormal];
}
completion:^(BOOL finished){
}];
Upvotes: 21
Reputation: 43330
NSTimer is absolutely no good as an animation tool, nor should it be used for timekeeping in general where precision is needed (frame rate). This blog post perfectly exemplifies my position on what to do about non-animateable UILabel properties, which should be sent off to the render server through CA, not an NSTimer. Instead of UILabel, you can use or wrap CATextLayer and animate it's foregroundColor
property in the standard animation block.
Upvotes: 6
Reputation: 2171
This isnt really an animation, but it would work.
Create a timer with whatever frequency you want to animate the colors with.
Have that timer call a method, changeColorOnButton
every time it fires.
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changeColorOnButton:) userInfo:nil repeats:YES];
Create an array of Colors, colorsArray
(either arrange the colors ahead of time, or add a bunch to an array and shuffle them to randomize. Create an int, intForColorInArray
.
In the changeColorOnButton method, do something like:
- (void) changeColorOnButton:(UIButton *) button {
UIColor *nextColor = [colorsArray objectAtIndex:intForColorArray];
[button.titleLabel.setTextColor:nextColor];
intForColorArray = (intForColorArray +1) % [colorsArray count];
}
Upvotes: -2
Reputation: 1000
Obviously you would need to use
[UIButton setTitleColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0] forState:UIControlStateNormal];
As for actually getting it to animate and change color over time, you'd need to create an instance of NSTimer and use the @selector paramater to point it to a specific method to run. Every time the timer runs, it triggers the method which then changes the color of your UIButton.
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateCounter:)
userInfo:nil
repeats:YES];
Also check out: http://servin.com/iphone/iPhone-Timers-and-Animated-Views.html
Upvotes: 0