gpt4 book ai didi

swift - 单击任意按钮即可启动计时器

转载 作者:行者123 更新时间:2023-11-30 13:48:17 25 4
gpt4 key购买 nike

var nsTimer = NSTimer()

override func viewDidLoad() {

super.viewDidLoad()



// Do any additional setup after loading the view.
}

override func viewDidDisappear(animated: Bool) {
self.nsTimer.invalidate()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
nsTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updatetime"), userInfo: nil, repeats: true);
}

func updateTime() {
counter = counter + 0.1
timer.text = String(format: "%.1f", counter) + "s"
}

有人知道吗?当我单击任何按钮计时器启动时,当我单击另一个按钮计时器包含

最佳答案

您必须创建一个 IBAction 并将所有按钮链接到它。

@IBAction func buttonTapped(sender: AnyObject) {
//this will check if your timer is running if not the start the timer, if the timer is already running do nothing and keep timer running
if !nsTimer.Enabled {
nsTimer.Start()
}
}

确保将所有按钮连接到此方法并从 TouchBegan 中删除代码

关于swift - 单击任意按钮即可启动计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34640966/

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