Animate text change in UILabel

后端 未结 14 1274
[愿得一人]
[愿得一人] 2020-11-28 17:56

I\'m setting a new text value to a UILabel. Currently, the new text appears just fine. However, I\'d like to add some animation when the new text appears. I\

14条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 18:50

    Swift 4.2 solution (taking 4.0 answer and updating for new enums to compile)

    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)
        }
    }
    
    func updateLabel() {
    myLabel.fadeTransition(0.4)
    myLabel.text = "Hello World"
    }
    

提交回复
热议问题