我想让我的标签从rLabel.text = "\(update) free trial"
起每5秒钟更改一次文本,再等5秒钟,然后rLabel.text = "upgrade now!"
〜重复进行更改。
rLabel.text = "\(update) free trial"
if daysLeft <= 5 {
let timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
timer.fire()
}
} else {
rLabel.text = "\(update) free trial"
}
@objc func update() {
rLabel.text = "upgrade now!"
}
答案 0 :(得分:0)
您只需使用布尔值即可每5秒切换一次文本
override func viewDidLoad() {
let timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
}
var isShowingText1 = false
@objc func updateText(){
rLabel.text = isShowingText1 ? "Text 2" : "Text 1"
isShowingText1 = !isShowingText1
}
答案 1 :(得分:0)
您可以通过拥有index
属性来跟踪当前文本。另外,您可以将Timer
与block
一起使用,并避免使用Obj-C-ish #selector
var index = 0 {
didSet {
if index > 1 { index = 0 }
}
}
var text: String {
return ["\(update) free trial", "upgrade now!"][index]
}
func foo() {
rLabel.text = text
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
self.index += 1
self.rLabel.text = self.text
}
}