gpt4 book ai didi

ios - 为定时器添加功能

转载 作者:行者123 更新时间:2023-11-30 14:05:59 26 4
gpt4 key购买 nike

如果我想在计时器达到 0 时尽快添加一个函数。有人有什么建议吗?这是我想要实现它的代码。

class ViewController: UIViewController {
@IBOutlet weak var Counterlabel: UILabel!
var Nbr = 51
var timerValue = 52
var timer = NSTimer()

override func viewDidLoad() {
super.viewDidLoad()
let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("Counter"), userInfo: nil, repeats: true)
}

@IBAction func Btn1(sender: AnyObject) {
Nbr = timerValue - 1
timerValue = timerValue - 1;
}

func Counter(){
Nbr -= 1
Counterlabel.text = "\(Nbr)"
}
}

最佳答案

根据您的计时器调用延迟函数,如下所示,

func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}

delay(Double(self.Nbr)) {
//Do your work here
}

以上函数引用自here

关于ios - 为定时器添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32405193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com