Is it possible to animate UILabel's textcolor change?

3 Solutions Collect From Internet About “Is it possible to animate UILabel's textcolor change?”

Try this

[UIView transitionWithView:myLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    label.textColor = [UIColor redColor];
} completion:^(BOOL finished) {
}];

I write down the code for Objective-C and Swift

animation types

typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn               = 1 << 16,
UIViewAnimationOptionCurveEaseOut              = 2 << 16,
UIViewAnimationOptionCurveLinear               = 3 << 16,

UIViewAnimationOptionTransitionNone            = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
} NS_ENUM_AVAILABLE_IOS(4_0);

coding for Objective-C

[UIView transitionWithView:myLabel duration:0.20 options: UIViewAnimationOptionTransitionFlipFromBottom animations:^{
myLabel.textColor = [UIColor redColor];

} completion:^(BOOL finished) {
}];

coding for Swift

  UIView.transition(with: myLabel, duration: 0.20, options: .transitionFlipFromBottom, animations: {() -> Void in
        self.myLabel.textColor = UIColor.red
    }, completion: {(_ finished: Bool) -> Void in
    })

Even though you say the accepted answer works, (1) you need to use TransitionCrossDissolve instead of CurveEaseInOut; (2) while testing, I noticed that in Swift, it seems as if you can’t add a subview immediately before animating it. (I think it’s a bug.)

Seeing as myLabel appears to be local in your example (since global variables would have to be written as self.myLabel within the block closure), there’s a good chance you’ve added the myLabel subview within the same method as the animation and without a delay.

So if you still run into problems, I recommend (1) making myLabel global and (2) adding a delay between adding the subview and performing the animation on that subview, ex:

    self.view.addSubview(myLabel)
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "animate", userInfo: nil, repeats: false)
}

func animate() {
    UIView.transitionWithView(myLabel, duration: 5.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
        self.myLabel.textColor = UIColor.redColor()
        }, completion:nil)
}