Animate text change in UILabel

后端 未结 14 1256
[愿得一人]
[愿得一人] 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

    With Swift 5, you can choose one of the two following Playground code samples in order to animate your UILabel's text changes with some cross dissolve animation.


    #1. Using UIView's transition(with:duration:options:animations:completion:) class method

    import UIKit
    import PlaygroundSupport
    
    class ViewController: UIViewController {
    
        let label = UILabel()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            label.text = "Car"
    
            view.backgroundColor = .white
            view.addSubview(label)
    
            label.translatesAutoresizingMaskIntoConstraints = false
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:)))
            view.addGestureRecognizer(tapGesture)
        }
    
        @objc func toggle(_ sender: UITapGestureRecognizer) {
            let animation = {
                self.label.text = self.label.text == "Car" ? "Plane" : "Car"
            }
            UIView.transition(with: label, duration: 2, options: .transitionCrossDissolve, animations: animation, completion: nil)
        }
    
    }
    
    let controller = ViewController()
    PlaygroundPage.current.liveView = controller
    

    #2. Using CATransition and CALayer's add(_:forKey:) method

    import UIKit
    import PlaygroundSupport
    
    class ViewController: UIViewController {
    
        let label = UILabel()
        let animation = CATransition()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            label.text = "Car"
    
            animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            // animation.type = CATransitionType.fade // default is fade
            animation.duration = 2
    
            view.backgroundColor = .white
            view.addSubview(label)
    
            label.translatesAutoresizingMaskIntoConstraints = false
            label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(toggle(_:)))
            view.addGestureRecognizer(tapGesture)
        }
    
        @objc func toggle(_ sender: UITapGestureRecognizer) {
            label.layer.add(animation, forKey: nil) // The special key kCATransition is automatically used for transition animations
            label.text = label.text == "Car" ? "Plane" : "Car"
        }
    
    }
    
    let controller = ViewController()
    PlaygroundPage.current.liveView = controller
    

提交回复
热议问题