Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

ios - Resuming an NSTimer in Swift

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)"
}

Thanks in advance. Anton

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use a CADisplayLink for two reasons:

  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.

  2. You can pause and resume a 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)
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...