Last active
November 1, 2023 23:57
-
-
Save boherna/9e3d223be4c8f2491a87 to your computer and use it in GitHub Desktop.
To animate (fade) text change in UILabel (SWIFT)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The proper way to fade a UILabel (or any UIView for that matter) is to use a Core Animation Transition. This will not flicker, nor will it fade to black if the content is unchanged. | |
A portable and clean solution is to use a Extension in Swift (invoke prior changing visible elements) | |
// Usage: insert view.fadeTransition right before changing content | |
extension UIView { | |
func fadeTransition(duration:CFTimeInterval) { | |
let animation:CATransition = CATransition() | |
animation.timingFunction = CAMediaTimingFunction(name: | |
kCAMediaTimingFunctionEaseInEaseOut) | |
animation.type = kCATransitionFade | |
animation.duration = duration | |
self.layer.addAnimation(animation, forKey: kCATransitionFade) | |
} | |
} | |
Invocation looks like this: | |
// This will fade | |
aLabel.fadeTransition(0.4) | |
aLabel.text = xxx |
This however did work:
UILabel.animate(withDuration: 0.5,
delay : 1.0,
options : [.repeat, .autoreverse, .curveEaseOut],
animations : { textLabel.alpha = 0.0 }) { (finished) in textLabel.alpha = 1.0 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola.
I tidied this up for XCode 10 (yea, I know) specifically for UILabel use, but it's still not working for me: