swift 3中标签中文本的时间延迟

时间:2017-05-31 11:26:00

标签: ios animation

我有一个标签。我有3个字符串。我需要在同一个标​​签上显示3个字符串的文本,在无限循环中延迟10秒。如何在swift 3中使用简单的动画解决这个问题?

2 个答案:

答案 0 :(得分:1)

这是我的解决方案。只需将UILabel连接到IBOutlet

即可
class ViewController: UIViewController {

    @IBOutlet weak var textLabel: UILabel!
    let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"]
    let delayTime = 10.0
    var counter = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true)
        timer.fire()
    }

    func changeDisplayedText() {
        textLabel.text = messages[counter % messages.count]
        counter += 1
    }
}

答案 1 :(得分:0)

这将适用于您,正确连接您的插座并在阵列中声明这些字符串并加载定时器更改。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var label: UILabel!

var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"]
var scrollIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true)
    timer.fire()
}

func myDelayedFunction()-> Void {
let count = self.array.count
    if scrollIndex == count {
        scrollIndex = 0
    }
    if scrollIndex < count {
        if count > 1{
            self.label.text = array[scrollIndex]
            self.scrollIndex += 1
        }
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}