ios – WKInterfaceTimer用作倒计时开始和停止的计时器

概述我正在尝试创建一个倒计时x分钟和y秒的计时器. 我正在计算秒数并创建InterfaceTimer,如下所示: timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue 1))) timer.stop() 在那之后我一直停下来并一次又一次地启动它,但是随着“时间(现在)不停止”,值突然减少. 例如:如果计时器显示:55,我启动它3秒并 我正在尝试创建一个倒计时x分钟和y秒的计时器.
我正在计算秒数并创建InterfaceTimer,如下所示:
timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue 1)))
timer.stop()

在那之后我一直停下来并一次又一次地启动它,但是随着“时间(现在)不停止”,值突然减少.
例如:如果计时器显示:55,我启动它3秒并停止它,它显示:52,我等待10秒然后再次启动它,它从42开始.

我无法保存当前在WKInterfaceTimer中的值,因此我可以从同一点重新开始.我试过的一切都行不通.有没有人使用计时器,它停止后保持相同的值?

解决方法 是的,Watchkit计时器有点……笨拙……绝对不是很直观.但那只是我的个人意见

每次用户选择恢复计时器时,您都必须继续设置日期/计时器.

请记住,您还需要一个内部NSTimer来跟踪事物,因为当前的WatchKit计时器只是用于显示而没有附加任何真正的逻辑.

也许这样的事情……它不优雅.但它的确有效

@IBOutlet weak var WKTimer: WKInterfaceTimer! //watchkit timer that the user will see        var myTimer : NSTimer?  //internal timer to keep track         var isPaused = false //flag to determine if it is paused or not        var elapsedtime : NSTimeInterval = 0.0 //time that has passed between pause/resume        var startTime = NSDate()        var duration : NSTimeInterval = 45.0 //arbitrary number. 45 seconds       overrIDe func willActivate(){           super.willActivate()           myTimer = NSTimer.scheduledTimerWithTimeInterval(duration,target: self,selector: Selector("timerDone"),userInfo: nil,repeats: false)           WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))            WKTimer.start()       }    @IBAction func pauseResumepressed() {        //timer is paused. so unpause it and resume countdown        if isPaused{            isPaused = false            myTimer = NSTimer.scheduledTimerWithTimeInterval(duration - elapsedtime,repeats: false)            WKTimer.setDate(NSDate(timeIntervalSinceNow: duration - elapsedtime))            WKTimer.start()            startTime = NSDate()            pauseResumebutton.setTitle("Pause")          }          //pause the timer          else{                isPaused = true                //get how much time has passed before they paused it                let paused = NSDate()                elapsedtime += paused.timeIntervalSinceDate(startTime)                //stop watchkit timer on the screen                WKTimer.stop()                //stop the ticking of the internal timer                myTimer!.invalIDate()                //do whatever UI changes you need to                pauseResumebutton.setTitle("Resume")            }        }      func timerDone(){           //timer done counting down      }
总结

以上是内存溢出为你收集整理的ios – WKInterfaceTimer用作倒计时开始和停止的计时器全部内容,希望文章能够帮你解决ios – WKInterfaceTimer用作倒计时开始和停止的计时器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存