Created
February 5, 2019 07:33
-
-
Save masuhara/122c143115f2d54f0609f96cb7ddadf6 to your computer and use it in GitHub Desktop.
TextView TypeWriter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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