Animate text change in UILabel

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

    since iOS4 it can be obviously done with blocks:

    [UIView animateWithDuration:1.0
                     animations:^{
                         label.alpha = 0.0f;
                         label.text = newText;
                         label.alpha = 1.0f;
                     }];
    
    0 讨论(0)
  • 2020-11-28 18:35

    Swift 2.0:

    UIView.transitionWithView(self.view, duration: 1.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
        self.sampleLabel.text = "Animation Fade1"
        }, completion: { (finished: Bool) -> () in
            self.sampleLabel.text = "Animation Fade - 34"
    })
    

    OR

    UIView.animateWithDuration(0.2, animations: {
        self.sampleLabel.alpha = 1
    }, completion: {
        (value: Bool) in
        self.sampleLabel.alpha = 0.2
    })
    
    0 讨论(0)
  • 2020-11-28 18:35

    There is one more solution to achieve this. It was described here. The idea is subclassing UILabel and overriding action(for:forKey:) function in the following way:

    class LabelWithAnimatedText: UILabel {
        override var text: String? {
            didSet {
                self.layer.setValue(self.text, forKey: "text")
            }
        }
    
        override func action(for layer: CALayer, forKey event: String) -> CAAction? {
            if event == "text" {
                if let action = self.action(for: layer, forKey: "backgroundColor") as? CAAnimation {
                    let transition = CATransition()
                    transition.type = kCATransitionFade
    
                    //CAMediatiming attributes
                    transition.beginTime = action.beginTime
                    transition.duration = action.duration
                    transition.speed = action.speed
                    transition.timeOffset = action.timeOffset
                    transition.repeatCount = action.repeatCount
                    transition.repeatDuration = action.repeatDuration
                    transition.autoreverses = action.autoreverses
                    transition.fillMode = action.fillMode
    
                    //CAAnimation attributes
                    transition.timingFunction = action.timingFunction
                    transition.delegate = action.delegate
    
                    return transition
                }
            }
            return super.action(for: layer, forKey: event)
        }
    }
    

    Usage examples:

    // do not forget to set the "Custom Class" IB-property to "LabelWithAnimatedText"
    // @IBOutlet weak var myLabel: LabelWithAnimatedText!
    // ...
    
    UIView.animate(withDuration: 0.5) {
        myLabel.text = "I am animated!"
    }
    
    myLabel.text = "I am not animated!"
    
    0 讨论(0)
  • 2020-11-28 18:40

    The system default values of 0.25 for duration and .curveEaseInEaseOut for timingFunction are often preferable for consistency across animations, and can be omitted:

    let animation = CATransition()
    label.layer.add(animation, forKey: nil)
    label.text = "New text"
    

    which is the same as writing this:

    let animation = CATransition()
    animation.duration = 0.25
    animation.timingFunction = .curveEaseInEaseOut
    label.layer.add(animation, forKey: nil)
    label.text = "New text"
    
    0 讨论(0)
  • 2020-11-28 18:43

    Swift 4

    The proper way to fade a UILabel (or any UIView for that matter) is to use a Core Animation Transition. This will not flicker, nor will it fade to black if the content is unchanged.

    A portable and clean solution is to use a Extension in Swift (invoke prior changing visible elements)

    // Usage: insert view.fadeTransition right before changing content
    
    
    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)
        }
    }
    

    Invocation looks like this:

    // This will fade
    aLabel.fadeTransition(0.4)
    aLabel.text = "text"
    


    ► Find this solution on GitHub and additional details on Swift Recipes.

    0 讨论(0)
  • 2020-11-28 18:47

    Swift 4.2 version of SwiftArchitect's solution above (works great):

        // Usage: insert view.fadeTransition right before changing content    
    
    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)
            }
        }
    

    Invocation:

        // This will fade
    
    aLabel.fadeTransition(0.4)
    aLabel.text = "text"
    
    0 讨论(0)
提交回复
热议问题