gpt4 book ai didi

ios - 快速激活停止后,秒表上的开始按钮会重置计时器

转载 作者:行者123 更新时间:2023-11-30 13:23:03 27 4
gpt4 key购买 nike

为什么按下停止按钮后,秒表上的开始按钮会重置计时器?一切正常,直到我停止计时器然后重新启动它。它应该从停止的地方继续,但它会将计时器重置为 0。这是我的代码:

ViewController 类:UIViewController {

var timer = NSTimer()
var startTime = NSTimeInterval()

@IBOutlet var displayTimeLabel: UILabel!

@IBAction func start(sender: AnyObject) {

if !timer.valid {

timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)

}

startTime = NSDate.timeIntervalSinceReferenceDate()

}

@IBAction func stop(sender: AnyObject) {

timer.invalidate()
}

func updateTime() {

let currentTime = NSDate.timeIntervalSinceReferenceDate()
var elapsedTime: NSTimeInterval = currentTime - startTime

let hours = UInt8(elapsedTime / 3600)
elapsedTime -= (NSTimeInterval(hours) * 3600)

let minutes = UInt8(elapsedTime / 60)
elapsedTime -= (NSTimeInterval(minutes) * 60)

let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)

let fraction = UInt8(elapsedTime * 100)

let strHours = String(format: "%02d", hours)
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", fraction)

displayTimeLabel.text = "\(strHours):\(strMinutes):\(strSeconds):\(strFraction)"

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

每次调用 start 方法时,您都会设置 startTime。您只想在第一次调用时执行此操作。您可以使用选项来正确确定状态,并且仅在 startTime 最初为零时设置它。

然后,您可以创建一个重置按钮,其函数只需设置 startDate = nil

class ViewController: UIViewController {

var timer : NSTimer?
var startTime? : NSTimeInterval?
var accumulatedTime = NSTimeInterval()

@IBOutlet var displayTimeLabel: UILabel!

@IBAction func start(sender: AnyObject) {

guard self.timer == nil else {
return
}

self.startTime = NSDate.timeIntervalSinceReferenceDate()

self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.updateTime), userInfo: nil, repeats: true)
}

@IBAction func stop(sender: AnyObject) {

guard self.timer != nil else {
return
}

self.timer!.invalidate()
self.timer = nil
}

func updateTime() {

let currentTime = NSDate.timeIntervalSinceReferenceDate()
let intervalTime = currentTime - startTime!
self.startTime = currentTime
self.accumulatedTime += intervalTime

var elapsedTime = self.accumulatedTime

let hours = UInt8(elapsedTime / 3600)
elapsedTime -= (NSTimeInterval(hours) * 3600)

let minutes = UInt8(elapsedTime / 60)
elapsedTime -= (NSTimeInterval(minutes) * 60)

let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)

let fraction = UInt8(elapsedTime * 100)

let timeString = String(format:"%02d:%02d:%02d.%02d",hours,minutes,seconds,fraction)
displayTimeLabel.text = timeString

}
}

关于ios - 快速激活停止后,秒表上的开始按钮会重置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556882/

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