i'm trying to make animation typing words in label. With short text everything works good, but if i put a bit longer it starts writing by parts of the word, not by one letter. whats wrong in my code? And how to fix it?
extension UILabel {
func animate(newText: String, characterDelay: TimeInterval) {
DispatchQueue.main.async {
self.text = ""
for (index, character) in newText.enumerated() {
DispatchQueue.main.asyncAfter(deadline: .now() + characterDelay * Double(index)) {
self.text?.append(character)
self.fadeTransition(0.2)
}
}
}
}
}
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)
}
}
next in viewDidLoad I called func:
override func viewDidLoad() {
super.viewDidLoad()
Label.animate(newText: """
Здесь много текста.
""", characterDelay: 0.1)
}