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
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.
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.