我想设置Label的文本外观,以便它看起来像是在输入。我想知道在SWIFT中是否有相对简单的方法可以做到这一点。谢谢。
答案 0 :(得分:2)
试试这个:
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()
}
}
答案 1 :(得分:0)
我建议您使用SKAction.wait(forDuration :)代替计时器
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()
}
}
如果要获得类型编写器外观,请提前设置水平对齐方式
label.horizontalAlignmentMode = .left