停止打字机动画并在swift中显示全文

时间:2016-12-16 08:25:29

标签: ios arrays swift animation

我有以下代码来为UITextView中的打字机文本设置动画。 问题是动画从一开始就按字符开始,我希望它能够在按下按钮后显示全文。

动画开始后显示全文的最佳方式是什么?

var myText = Array("".characters)
var myCounter = 0
var timer:Timer?
var TextNumber = 0


func fireTimer(){


    timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: true)
}
func typeLetter(){
    if myCounter < myText.count {

        myTypeWriter.text = myTypeWriter.text! + String(myText[myCounter])
        let randomInterval = Double((arc4random_uniform(8)+1))/190
        timer?.invalidate()
        timer = Timer.scheduledTimer(timeInterval: randomInterval, target: self, selector: #selector(Main1.typeLetter), userInfo: nil, repeats: false)
    } else {
        timer?.invalidate()

    }
    myCounter += 1


}

@IBAction func showFullText(){
 //What should i put here?
}

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作,即使时间无效并将数组转换为字符串并放置此文本

   @IBAction func showFullText(){

         timer?.invalidate()

         let strFromArray = myText.joinWithSeparator(" ") 
         //In Swift 3, joinWithSeparator becomes joined(separator:)

         myTypeWriter.text = strFromArray

        }