Ios Xcode Swift秒表应用程序
我正在制作一个秒表应用程序,它当前在UILabel上显示分钟、秒和毫秒。它有一个“开始”按钮和一个“停止”按钮。代码如下。我如何增加时间?还有,当我停止它时,我如何使它继续它原来的位置?当前,如果我再次按start,它将重置并重新开始。稍后我将添加一个重置按钮Ios Xcode Swift秒表应用程序,ios,xcode,swift,timer,Ios,Xcode,Swift,Timer,我正在制作一个秒表应用程序,它当前在UILabel上显示分钟、秒和毫秒。它有一个“开始”按钮和一个“停止”按钮。代码如下。我如何增加时间?还有,当我停止它时,我如何使它继续它原来的位置?当前,如果我再次按start,它将重置并重新开始。稍后我将添加一个重置按钮 //Variables var startTime = NSTimeInterval() //Start Button @IBAction func start(sender: AnyObject) { let aSelector
//Variables
var startTime = NSTimeInterval()
//Start Button
@IBAction func start(sender: AnyObject) {
let aSelector : Selector = “updateTime”
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
//Stop Button
@IBAction func stop(sender: AnyObject) {
timer.invalidate()
}
//Update Time Function
func updateTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = currentTime - startTime
//calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
//calculate the seconds in elapsed time.
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
//find out the fraction of milliseconds to be displayed.
let fraction = UInt8(elapsedTime * 100)
//add the leading zero for minutes, seconds and millseconds and store them as string constants
let strMinutes = String(format: "%02d", minutes)
let strSeconds = String(format: "%02d", seconds)
let strFraction = String(format: "%02d", fraction)
//concatenate minuets, seconds and milliseconds as assign it to the UILabel
displayTimeLabel.text = “\(strMinutes):\(strSeconds):\(strFraction)”
}
你可以这样计算小时数
elapsedtime / 3600
请在此处阅读更多信息,以使您继续使用秒表: 创建
var stopTime:NSTimeInterval=0
调用stop()函数时,插入以下代码:
stopTime = elapsedTime
在start()函数中,替换为:
elapsedTime = (currentTime - startTime) + stopTime
或者可以使用NSDate()、NSDateFormatter()来解决此问题。
此处参考请使用描述您的问题,而不是包含代码的屏幕截图。这使复制和粘贴成为可能。您还可以配置。好的,对不起,谢谢!太好了,谢谢你。你知道如何使它继续依靠秒表计数,并且在我再次单击“开始”时不会重置吗?@ChallengerGuy请写信给我电子邮件我该怎么做?我对整个堆栈溢出的事情还不熟悉_temka@icloud.com