UILabel text animation as in UIButton click


Animating UILabel textcolor is not easy as it seems, normal animation won't work on this property. The below code snippet animates the UILabel textcolor as in button click.
Change the duration and UIViewAnimationOptions to play with it.
extension UILabel{
    func flashLabel() {
        func animate(duration:Double,alpha:CGFloat,completion:(()->())?){
            UIView.transition(with: self, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {
                self.textColor = self.textColor?.withAlphaComponent(alpha)
            }) { _ in
                completion?()
            }
        }
        animate(duration: 0.1, alpha: 0) {
            animate(duration: 0.3, alpha: 1, completion: nil)
        }
    }
}
Happy coding...

Comments

Popular posts from this blog

Swift Package Manager: Demystifying Targets and Libraries

IOS Simulator Crash report

Palindrome check