Typing animation: deleting characters

后端 未结 2 1030
情书的邮戳
情书的邮戳 2021-01-28 05:57

Here\'s a typing animation that adds up characters from an Array to the text of a text field at a fixed time interval:

@IBOutlet weak var textFieldMain: UITextFi         


        
相关标签:
2条回答
  • 2021-01-28 06:18

    I would rewrite the deleteLetter() method into something like this:

    func deleteLetter() {
        myString = myString.substringToIndex(myString.endIndex.predecessor())
        textFieldMain.text = myString
    
        typingAnimationTimer?.invalidate()
        if myString.characters.count > 0 {
            typingAnimationTimer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "deleteLetter", userInfo: nil, repeats: false)
        }
    }
    

    Edit: Looks like Zoltan already beat me to it.

    0 讨论(0)
  • 2021-01-28 06:24

    Why not do the following:

    func deleteLetter() {
        if textFieldMain.text != "" {
            textFieldMain.text = str.substringToIndex(textFieldMain.text.endIndex.predecessor())
        } else {
           typingAnimationTimer?.invalidate()
        }
    }
    

    By the way, if you start your timer with "repeats: true" in fireDeletionTimer() you don't need to recreate it again and again in deleteLetter()

    Also, stringByReplacingOccurrencesOfString() clears every instance of that letter in your text, not just the last one.

    0 讨论(0)
提交回复
热议问题