ios - Objective c中小时格式的倒数计时器
<p><p>我在我的 ViewController 中设置了一个计时器,当我给它任何静态数字时它工作正常。现在我想设置一个一小时的计时器,它以一秒递减并在达到零时失效。我已经写了一些代码,</p>
<pre><code>countInt=10;
self.lblTimer.text=;
timer=;
-(void)startCounter{
countInt -=1;
self.lblTimer.text=;
if (countInt==0) {
;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这个:</p>
<pre><code>import UIKit
class ViewController: UIViewController{
@IBOutlet weak var timerLbl: UILabel!
var timer = Timer()
@IBAction func pausePressed(_ sender: Any) {
timer.invalidate()
}
@IBAction func playPressed(_ sender: Any) {
if timer.isValid {
timer.invalidate()
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(reduceSeconds), userInfo: nil, repeats: true)
}
override func viewDidLoad() {
super.viewDidLoad()
timerLbl.text = "180"
timerLbl.font = UIFont.boldSystemFont(ofSize: 32)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func reduceSeconds() -> Void {
timerLbl.text = String(Int(timerLbl.text!)! - 1)
}
}
</code></pre>
<p>这将按 180 秒运行,计数器将减少 1(根据需要),您可以将其重置或设置为您喜欢的方式。</p>
<p><strong>注意:</strong></p>
<p><code>pausePressed</code> 是一个导航栏图标和对它的操作</p>
<p><code>playPressed</code> 也是一个导航栏图标和对它的操作</p></p>
<p style="font-size: 20px;">关于ios - Objective c中小时格式的倒数计时器,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47766344/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47766344/
</a>
</p>
页:
[1]