在Swift中恢复NSTimer [英] Resuming an NSTimer in Swift

查看:168
本文介绍了在Swift中恢复NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此遇到的其他问题没有帮助.我希望能够启动,暂停然后恢复NSTimer.我知道如何启动NSTimer.我也知道您不能暂停" NSTimer,但是可以使其无效.但是,如果我想在停止计时器的同时恢复它,该怎么办?这是代码:

The other questions I've seen on this didn't help. I want to be able to start, pause, and then resume the NSTimer. I know how to start the NSTimer. I also know that you can't 'pause' the NSTimer but you can invalidate it. But what would I have to do if I wanted to resume it keeping the same time I had when I stopped the timer? Here's the code:

var startTimer = NSTimeInterval()
var timer = NSTimer()
@IBOutlet var displaylabel: UILabel! 
@IBAction func start(sender: UIButton) {
        timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTime", userInfo: nil, repeats: true)
        startTimer = NSDate.timeIntervalSinceReferenceDate()
}
@IBAction func stop(sender: UIButton) {
timer.invalidate()
}
@IBAction func resume(sender: UIButton) {
// Code needed
}
func updateTime() {
    let currentTime = NSDate.timeIntervalSinceReferenceDate()
    var elapsedTime: NSTimeInterval = currentTime - startTimer
    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)
    let seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    let fraction = UInt8(elapsedTime * 100)
    let strMinutes = String(format: "%02d", minutes)
    let strSeconds = String(format: "%02d", seconds)
    let strFraction = String(format: "%02d", fraction)
    displaylabel.text = "\(strMinutes):\(strSeconds).\(strFraction)"
}

先谢谢了.安东

推荐答案

出于两个原因,您应该使用CADisplayLink:

You should use a CADisplayLink for two reasons:

  1. 您将尽可能频繁地更新屏幕,并且不会浪费过多的电池寿命来尝试尽可能多地更新屏幕.所有iOS设备(除了极少数情况下的iPad Pro除外)每秒最多更新屏幕60次.将计时器安排为0.01秒意味着您试图以每秒至少40次的频率更新屏幕.

  1. You'll update the screen as often as possible and not waste extra battery life trying to update it more often than possible. All iOS devices (except the iPad Pro under rare circumstances) update the screen at most 60 times per second. Scheduling your timer for 0.01 seconds means you're trying to update the screen at least 40 times per second more often than necessary.

您可以暂停和恢复CADisplayLink.

处理暂停需要一些思考.您要做的是记录暂停时间(与开始时间分开),然后再记录,将开始时间增加暂停后经过的时间.

Handling pause requires a little thought. What you want to do is record the time when you pause (separately from the start time), and then when you resume, increase the start time by the time elapsed since pausing.

class ViewController: UIViewController {

    private var startTime = NSTimeInterval(0)
    private var pauseTime = NSTimeInterval(0)
    private var displayLink: CADisplayLink!

    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        displayLink = CADisplayLink(target: self, selector: "displayLinkDidFire")
        displayLink.paused = true
        displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
    }

    @IBAction func startWasTapped(sender: AnyObject) {
        startTime = NSDate.timeIntervalSinceReferenceDate()
        displayLink.paused = false
    }

    @IBAction func resumeWasTapped(sender: AnyObject) {
        startTime += NSDate.timeIntervalSinceReferenceDate() - pauseTime
        displayLink.paused = false
    }

    @IBAction func stopWasTapped(sender: AnyObject) {
        pauseTime = NSDate.timeIntervalSinceReferenceDate()
        displayLink.paused = true
    }

    func displayLinkDidFire() {
        let elapsedTime = NSDate.timeIntervalSinceReferenceDate() - startTime
        label.text = String(format: "%02.0f:%05.2f", floor(elapsedTime / 60), elapsedTime)
    }

}

这篇关于在Swift中恢复NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆