【发布时间】:2017-05-22 02:32:18
【问题描述】:
在我的代码中,如果您按下 startButton 倒计时将从 60 - 0 开始。我想要做的就是从 3 - 0 倒计时,然后从 60 - 0 倒计时。想想它,标记,设置,然后启动倒计时计时器,从 60 秒到 0。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var timerLabel: UILabel!
var seconds = 60
var timer = Timer()
var isTimerRunning = false
var resumeTapped = false
@IBAction func startButtonTapped(_ sender: UIButton) {
if isTimerRunning == false {
runTimer()
self.startButton.isEnabled = false
}
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
isTimerRunning = true
}
@IBAction func resetButtonTapped(_ sender: UIButton) {
timer.invalidate()
seconds = 60
timerLabel.text = timeString(time: TimeInterval(seconds))
isTimerRunning = false
}
//MARK: - Public Method
func updateTimer(){
if seconds < 1 {
timer.invalidate()
//Send alert to indicate time's up.
} else {
seconds -= 1
timerLabel.text = timeString(time: TimeInterval(seconds))
}
}
func timeString(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}}
【问题讨论】:
-
这里面临什么问题?
-
我正在尝试链接 2 个倒数计时器 3-0 和 60-0。
标签: ios if-statement timer swift3 countdown