Typewriter effect text animation

后端 未结 3 1119
悲哀的现实
悲哀的现实 2020-12-03 09:19

I\'m trying to create a typewriter animation effect with a UILabel, but can\'t find any answers. Is the UILabel the correct object to use? I want the text to print to the sc

相关标签:
3条回答
  • 2020-12-03 09:55

    update: Xcode 7.0 GM • Swift 2.0

    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var myTypeWriter: UITextField!
        let myText = Array("Hello World !!!".characters)
        var myCounter = 0
        var timer:NSTimer?
        func fireTimer(){
            timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "typeLetter", userInfo: nil, repeats: true)
        }
        func typeLetter(){
            if myCounter < myText.count {
                myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter])
                let randomInterval = Double((arc4random_uniform(8)+1))/20
                timer?.invalidate()
                timer = NSTimer.scheduledTimerWithTimeInterval(randomInterval, target: self, selector: "typeLetter", userInfo: nil, repeats: false)
            } else {
                timer?.invalidate()
            }
            myCounter++
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            fireTimer()
            // Do any additional setup after loading the view, typically from a nib.
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
    0 讨论(0)
  • 2020-12-03 09:55

    I have written a subclass of UILabel called CLTypingLabel, available on GitHub. This should do what you want.

    After installing CocoaPods, add the following like to your Podfile to use it:

    pod 'CLTypingLabel'
    

    Sample Code

    Change the class of a label from UILabel to CLTypingLabel;

    @IBOutlet weak var myTypeWriterLabel: CLTypingLabel!
    

    At runtime, set text of the label will trigger animation automatically:

    myTypeWriterLabel.text = "This is a demo of typing label animation..."
    

    You can customize time interval between each character:

    myTypeWriterLabel.charInterval = 0.08 //optional, default is 0.1
    

    You can pause the typing animation at any time:

    myTypeWriterLabel.pauseTyping() //this will pause the typing animation
    myTypeWriterLabel.continueTyping() //this will continue paused typing animation
    

    Also there is a sample project that comes with cocoapods

    0 讨论(0)
  • 2020-12-03 09:56

    Based on this Answer: Letter by letter animation for UILabel?

    I've updated it to Swift 4 and solved the CPU animation problem with DispatchWorkItem in order to create a queue.

    Swift 4

    extension UILabel {
        func setTextWithTypeAnimation(typedText: String, characterDelay: TimeInterval = 5.0) {
            text = ""
            var writingTask: DispatchWorkItem?
            writingTask = DispatchWorkItem { [weak weakSelf = self] in
                for character in typedText {
                    DispatchQueue.main.async {
                        weakSelf?.text!.append(character)
                    }
                    Thread.sleep(forTimeInterval: characterDelay/100)
                }
            }
    
            if let task = writingTask {
                let queue = DispatchQueue(label: "typespeed", qos: DispatchQoS.userInteractive)
                queue.asyncAfter(deadline: .now() + 0.05, execute: task)
            }
        }
    
    }
    

    Usage

    label.setTextWithTypeAnimation(typedText: text, characterDelay:  10) //less delay is faster
    
    0 讨论(0)
提交回复
热议问题