小编典典

打字机效果文字动画

swift

我正在尝试使用UILabel创建打字机动画效果,但找不到任何答案。UILabel是正确使用的对象吗?我希望文本将字符串数组打印到屏幕上,例如“登录…正在打开文件夹…正在重新启动系统..”等。我应该提一下,我是编码的新手,并且尝试过搜索文档和API参考,但没有运气。如果值得一提,我目前正在学习SWIFT


阅读 328

收藏
2020-07-07

共1个答案

小编典典

更新: Xcode 7.0 GM•Swift 2.0

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var myTypeWriter: UITextField!
    let myText = Array("Hello World !!!".characters)
    var myCounter = 0
    var timer:NSTimer?
    func fireTimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "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))/20
            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(randomInterval, target: self, selector: "typeLetter", userInfo: nil, repeats: false)
        } else {
            timer?.invalidate()
        }
        myCounter++
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        fireTimer()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
2020-07-07