Countdown only in seconds (form 00:20 to 0) using NSTimer

I need to have a label countdown from 20 seconds to zero and start over. This is my first Swift project, and I'm trying to use NSTimer.scheduledTimerWithTimeInterval . This countdown should be performed in a loop a certain number of times.

I find it difficult to implement the Start and Start again method (loop). I basically do not find a way to start the clock for the 20s, and when it is over, start it again.

I would be grateful for any idea on how to do this Wagner

  @IBAction func startWorkout(sender: AnyObject) { timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("countDownTime"), userInfo: nil, repeats: true) startTime = NSDate.timeIntervalSinceReferenceDate() } func countDownTime() { var currentTime = NSDate.timeIntervalSinceReferenceDate() //Find the difference between current time and start time. var elapsedTime: NSTimeInterval = currentTime - startTime //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 strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds) let strFraction = fraction > 9 ? String(fraction):"0" + String(fraction) //concatenate minuets, seconds and milliseconds as assign it to the UILabel timeLabel.text = "\(strSeconds):\(strFraction)" } 
0
source share
4 answers

Now you need to set the endTime date to 20s and just check the dateIntervalSinceNow date. Once timeInterval reaches 0, you set it again to 20 seconds.

 import UIKit class ViewController: UIViewController { @IBOutlet weak var strTimer: UILabel! var endTime = NSDate().dateByAddingTimeInterval(20) var timer = NSTimer() func updateTimer() { let remaining = endTime.timeIntervalSinceNow strTimer.text = remaining.time if remaining <= 0 { endTime = NSDate().dateByAddingTimeInterval(20) } } override func viewDidLoad() { super.viewDidLoad() strTimer.text = "20:00" timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "updateTimer", userInfo: nil, repeats: true) NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } extension NSTimeInterval { var time:String { return String(format:"%02d:%02d", Int((self) % 60 ),Int(self*100 % 100 ) ) } } 
+1
source

In your countdown (), change the start time to the current time when your elapsed time reaches 20 seconds.

0
source

First of all, if you just loop around without stopping, you can simply use the module to get seconds. That is, seconds % 20 will just jump from 19.9 to 0.0. And thus, if you count, you would calculate seconds - seconds % 20 , which would reach 20 when it reaches zero. Again and again. That's what you need?

For leading zeros you can use: String(format: "%02d:%02d", seconds, fraction) . Pay attention to formatting: here seconds and fraction are integers.

But if you need to stop the timer, you must keep track of the previously counted seconds and reset startTime each time it starts. Each time you stop, you will need to add current seconds to seconds. Do I make sense?

0
source

To minimize processing, you can create two timers. One timer for 20 seconds and another timer for how often you want to update the interface. It would be difficult to see 100 frames per second. If you check every 0.01, your code is less accurate. The guide is really helpful. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/ When you no longer use the invalidate timer and set to nil. There are other synchronization features.

0
source

All Articles