Skip to content

Instantly share code, notes, and snippets.

@mspvirajpatel
Created February 19, 2018 03:59
Show Gist options
  • Save mspvirajpatel/3ccff1d1a415691033b8df96e60dc398 to your computer and use it in GitHub Desktop.
Save mspvirajpatel/3ccff1d1a415691033b8df96e60dc398 to your computer and use it in GitHub Desktop.
Animate text change in UILabel (Label Fade Animation)
// Usage: insert view.fadeTransition right before changing content
extension UIView {
func fadeTransition(_ duration:CFTimeInterval) {
let animation = CATransition()
animation.timingFunction = CAMediaTimingFunction(name:
kCAMediaTimingFunctionEaseInEaseOut)
animation.type = kCATransitionFade
animation.duration = duration
layer.add(animation, forKey: kCATransitionFade)
}
}
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var count = 0
@IBAction func doAction(_ sender: UIButton) {
// This will fade:
label.fadeTransition(0.4)
count += 1
label.text = "\(count)"
}
}
@usman-whizpool
Copy link

swift 5.0 code

extension UIView {
func fadeTransition(_ duration:CFTimeInterval) {
let animation = CATransition()
animation.timingFunction = CAMediaTimingFunction(name:
CAMediaTimingFunctionName.easeInEaseOut)
animation.type = CATransitionType.fade
animation.duration = duration
layer.add(animation, forKey: CATransitionType.fade.rawValue)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment