Reputation: 383
I want to to animate appearance of Label's text so that it looks like it is being typed in at the moment. I was wondering if there is a relatively easy way to do so in SWIFT. Thank you.
Upvotes: 3
Views: 1707
Reputation: 63
I would suggest you use SKAction.wait(forDuration:) instead of timer
extension SKLabelNode{
func startTyping(_ duration:TimeInterval, completion:(()->Void)?){
guard let text = self.text else{return}
self.text = ""
var index = 0
var block:(() -> Void)!
block = {
index += 1
if index > text.count{
completion?()
return
}else{
let action = SKAction.sequence([SKAction.wait(forDuration: duration), SKAction.run{self.text = String(text.prefix(index))}])
self.run(action, completion: block)
}
}
block()
}
}
If you want to get a type writer look, set the horizontal alignment in advance
label.horizontalAlignmentMode = .left
Upvotes: 3
Reputation: 1042
Try this:
class GameScene : SKScene {
let text = ["G", "a", "m", "e"]
var labelText = ""
let labelNode = SKLabelNode()
var calls : Int = 0
var timer : NSTimer!
override func didMoveToView(view: SKView) {
timer = NSTimer.scheduledTimerWithTimeInterval(YOUR_DESIRED_INTERVAL, target: self, selector: #selector(self.updateLabelText), userInfo: nil, repeats: true)
labelNode.text = labelText
self.addChild(labelNode)
}
func updateLabelText() {
labelText += text[calls]
labelNode.text = labelText
calls += 1
if calls == text.count + 1 {
timer.invalidate()
}
}
Upvotes: 3