Skip to content

Instantly share code, notes, and snippets.

@masuhara
Created February 5, 2019 07:33
Show Gist options
  • Save masuhara/122c143115f2d54f0609f96cb7ddadf6 to your computer and use it in GitHub Desktop.
Save masuhara/122c143115f2d54f0609f96cb7ddadf6 to your computer and use it in GitHub Desktop.
TextView TypeWriter
extension UITextView {
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)
}
}
}
// 使い方
TEXTVIEW.setTextWithTypeAnimation(typedText: "STRING", characterDelay: 10.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment