Is it possible to change the animation of text in UILabel?

UIView.animateWithDuration(5, animations: { myLabel.textColor = UIColor.redColor() }) 

The textcolor label just changes instantly

+5
source share
3 answers

try it

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

I write code for Objective-C and Swift

types of animation

 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); 

encoding 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 }) 
+5
source

Even if you say that the accepted answer works, (1) you need to use TransitionCrossDissolve instead of CurveEaseInOut ; (2) during testing, I noticed that in Swift it seems that you cannot add a subview just before the animation. (I think this is a mistake.)

Seeing that myLabel turns out to be local in your example (since global variables should be written as self.myLabel within the closure of the block), there is a good chance that you added subview myLabel to the same method as the animation and without delay.

So, if you are still having problems, I recommend (1) making myLabel global and (2) adding a delay between adding a subview and performing the animation in this view, for example:

  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) } 
+3
source

Source: https://habr.com/ru/post/1209588/


All Articles