-1
UIView.animateWithDuration(self.fadeTime, delay: 0, 
    options: UIViewAnimationOptions.AllowUserInteraction, 
    animations: { [weak self] () -> Void in
    var randomIndex = Int(arc4random_uniform(UInt32(CONSTANTS.MainColorScheme.count)))
    self?.startButton.titleLabel!.textColor = CONSTANTS.MainColorScheme[randomIndex]
    }) { (stuff Bool) -> Void in
}

This doesn't seem to work...it just "jumps" to the next color. It doesn't fade. However, if I apply the same approach to self.view.backgroundColor, my code works.

Is there an alternative?

mixel
  • 25,177
  • 13
  • 126
  • 165
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • Try `UIViewAnimationOptions.TransitionCrossDissolve` in place of animation options. – iphonic Jul 28 '15 at 07:03
  • Similar question http://stackoverflow.com/q/2426614/746347 – mixel Jul 28 '15 at 07:17
  • Check out this article http://webcache.googleusercontent.com/search?q=cache:PuadPDYxc1MJ:https://corecocoa.wordpress.com/2011/10/04/animatable-text-color-of-uilabel – mixel Jul 28 '15 at 07:18

3 Answers3

4

UILabel.textColor does not support animation. But you can animate CATextLayer:

Swift:

let textLayer = CATextLayer()
textLayer.string = "Your text"
textLayer.foregroundColor = yourFirstColor
textLayer.frame = yourButton.bounds
yourButton.layer.addSublayer(textLayer)

UIView.animateWithDuration(1) {
    textLayer.foregroundColor = yourSecondColor
}

Objective C:

CATextLayer *textLayer = [CATextLayer layer];
[textLayer setString:@"Your text"];
[textLayer setForegroundColor:yourFirstColor];
[textLayer setFrame:yourButton.bounds];
[[yourButton layer] addSublayer:textLayer];

[UIView animateWithDuration:1 animations:^{
     textLayer.foregroundColor = yourSecondColor;
}];
mixel
  • 25,177
  • 13
  • 126
  • 165
1

Indeed, the UIView.animateWithDuration will work with UILabel backgroundColor, while for the textColor it won't because color doesn't animate with UIView animations. Then, how to achieve?

Solution 1:

You can go for CATextLayer instead of a UILabel and then animate the color. For objective-C you can refer to this - iPad: Animate UILabels color changing

Solution 2:

You can use NSTimer and decrease the UILabel alpha as the time passes

Solution 3:

Or simply you can go for "FadeIn - FadeOut" animation with UIView.transitionWithView

Community
  • 1
  • 1
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

Try this..

UIView.transitionWithView(YOURLABEL, duration: 0.325, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {

    // animation...

}, completion: { (fininshed: Bool) -> () in

    // completion...

})

Also check for other syntax here

Cheers! :)

Community
  • 1
  • 1
0yeoj
  • 4,500
  • 3
  • 23
  • 41