Created
February 19, 2018 03:59
-
-
Save mspvirajpatel/3ccff1d1a415691033b8df96e60dc398 to your computer and use it in GitHub Desktop.
Animate text change in UILabel (Label Fade Animation)
This file contains 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
// 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) | |
} | |
} |
This file contains 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
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)" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)
}
}