12

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.

tresnotas
  • 249
  • 5
  • 13

6 Answers6

21

Use transition with view

[UIView transitionWithView:backButton
                          duration:0.5f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            [backButton setTitleColor:[_peacock lighten:d.primary] forState:UIControlStateNormal];
                        }
                        completion:^(BOOL finished){
                        }];
Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
  • 1
    Works great. Also, you can use a parent view as main target, and then animate all the child views the way you want in the `animations` block – Peterdk Jul 14 '17 at 17:56
6

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.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
1

This is answered very well here. It basically uses this handy guy :

[UIView transitionWithView:duration:options:animations:^()completion:^()]; 
Arnaud
  • 7,259
  • 10
  • 50
  • 71
Lofar
  • 36
  • 3
0

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

Stuartsoft
  • 1,000
  • 1
  • 8
  • 20
  • Note that this is no replacement for a good ol CA animation. UIColor objects may not be animateable, but CGColor objects absolutely are. If you could subclass UILabel, then you could implement your own text rendering and use the inherent CA animations. – CodaFi Jul 15 '12 at 04:16
0

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)
Denis Kreshikhin
  • 8,856
  • 9
  • 52
  • 84
-2

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];
}
bkbeachlabs
  • 2,121
  • 1
  • 22
  • 33