Skip to content

Instantly share code, notes, and snippets.

@tularovbeslan
Created May 25, 2019 18:54
Show Gist options
  • Save tularovbeslan/75cb487d2249d7f32c5095179a57d129 to your computer and use it in GitHub Desktop.
Save tularovbeslan/75cb487d2249d7f32c5095179a57d129 to your computer and use it in GitHub Desktop.
An Example Of Animating SCNText With A Typewriter Like Effect
//1. Timer To Animate Our Text
var animationTimer: Timer?
//2. Variable To Store The Current Time
var time:Int = 0
/// Animates The Presentation Of SCNText To Give An Appearance Of A Typing Effect
///
/// - Parameters:
/// - textGeometry: SCNText
/// - text: String
/// - completed: () -> Void
func animateTextGeometry(_ textGeometry: SCNText, withText text: String, completed: @escaping () -> Void ){
//1. Get All The Characters From The Text
let characters = Array(text)
//2. Run The Animation
animationTimer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: true) { [weak self] timer in
//a. If The Current Time Doesnt Equal The Count Of Our Characters Then Continue To Animate Our Text
if self?.time != characters.count {
let currentText: String = textGeometry.string as! String
textGeometry.string = currentText + String(characters[(self?.time)!])
self?.time += 1
}else{
//b. Invalide The Timer, Reset The Variables & Escape
timer.invalidate()
self?.time = 0
completed()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment