Skip to content

Instantly share code, notes, and snippets.

@heckj
Created April 20, 2019 22:28
Show Gist options
  • Save heckj/e0c8072126e18c174dcd2db14657c159 to your computer and use it in GitHub Desktop.
Save heckj/e0c8072126e18c174dcd2db14657c159 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