New guy here teaching myself Swift. Building my first personal app and have hit a wall after several searches on here, youtube, and google. This is my first time posting a question (as I've been able to find my other answers on here).
I'm having issues with my timer updating on the UILabel. I've managed to find code on older versions of swift that get the timer to run and count down. I then figured out how to break the seconds down to minutes and seconds.
But what I find is when I run the app, the timer shows "30:0" (a different issue I need to figure out) and never counts down. When I leave the page in the simulator and come back, it's only then that the UILabel updates.
I know viewdidload only loads upon the first moment the page opens. I'm having a hard time figuring out how to get the UILabel to update every time a second changes. I'm not sure what code to implement.
Thank you so much!
import UIKit
var timer = Timer()
var timerDuration: Int = 1800
// This converts my timeDuration from seconds to minutes and seconds.
func secondsToHoursMinutesSeconds (seconds : Int) -> (h: Int, m : Int, s : Int) {
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}
class lyricWriteViewController: UIViewController {
//This takes the function to convert minutes and seconds and accepts an input, which I've chosen the variable timeDuration (which is currently 1800 seconds.
var theTimer = (h: 0, m: 0, s: 0)
#IBOutlet weak var countdownTimer: UILabel!
#IBOutlet weak var randomLyric: UILabel!
#IBOutlet weak var titleInput: UITextField!
#IBOutlet weak var lyricInput: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
//This line takes a random array number and shows it on the textlabel.
randomLyric.text = oneLiner
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
//This is the code the does the counting down
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(lyricWriteViewController.counter), userInfo: nil, repeats: true)
}
#objc func counter() {
timerDuration -= 1
// Below is the timer view I've created. It has converted seconds to minutes and seconds but the screen won't refresh. Also, when the seconds number hits zero, it does "0" instead of "00".
let displayTimer = "\(theTimer.m) : \(theTimer.s)"
countdownTimer.text = String(displayTimer)
//When the timer hits 0, it stops working so that it doesn't go into negative numbers
if timerDuration == 0 {
timer.invalidate()
}
}
func submitlyricsButton(_ sender: UIButton) {
//I will eventually tie this to a completed lyric tableview.
}
}
var timer: Timer?
var totalTime = 120
private func startOtpTimer() {
self.totalTime = 120
self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
print(self.totalTime)
self.lblTimer.text = self.timeFormatted(self.totalTime) // will show timer
if totalTime != 0 {
totalTime -= 1 // decrease counter timer
}
else {
if let timer = self.timer {
timer.invalidate()
self.timer = nil
}
}
}
func timeFormatted(_ totalSeconds: Int) -> String {
let seconds: Int = totalSeconds % 60
let minutes: Int = (totalSeconds / 60) % 60
return String(format: "%02d:%02d", minutes, seconds)
}
It is because you are not updating the theTimer value. As viewDidLoad() is called once it is not working fine, you need to update theTimer value after deducting 1 from it.
So move this line :
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
in counter() funtion after timerDuration -= 1. So your function should look like this :
#objc func counter() {
timerDuration -= 1
if timerDuration == 0 {
timer.invalidate()
} else {
theTimer = secondsToHoursMinutesSeconds(seconds: timerDuration)
let displayTimer = "\(theTimer.m) : \(theTimer.s)"
countdownTimer.text = String(displayTimer)
}
}
Also move all of this inside controller:
var timer = Timer()
var timerDuration: Int = 1800
// This converts my timeDuration from seconds to minutes and seconds.
func secondsToHoursMinutesSeconds (seconds : Int) -> (h: Int, m : Int, s : Int){
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)}
As timerDuration is global you will have to kill the app and run it again to see the timer working again.
Replace countdownTimer.text = String(displayTimer) with
DispatchQueue.main.async {
countdownTimer.text = String(displayTimer)
}
What I think is happening here is since countdownTimer.text = String(displayTimer) is not running on the main thread it is not updating immediately. It does however after a period of time (Like you said, when when you traverse the screen).
Related
I am creating a timer on my app in iOS which counts down from 20 minutes to 0 seconds, one second at a time. So far the timer works but only counts down for 20 seconds, not minutes. It also doesn't stop when it gets to zero. How can this be resolved?
import UIKit
class SkippingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startWorkoutButton: UIButton!
#IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var counter = 20.00
var isRunning = false
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(counter)"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
}
#IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !isRunning {
timer = Timer.scheduledTimer(timeInterval: -0.01, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
isRunning = true
}
}
#IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
timer.invalidate()
isRunning = false
}
#objc func updateTimer() {
counter -= 0.01
timeLabel.text = String(format: "%.01f", counter)
}
There's a couple of issues that I see with your code that prevent it from working the way you want.
First of all, if the timeInterval passed in the scheduledTimer method is negative, it will always lead to a timer being created that fires every 0.1 milliseconds (source: documentation).
What you want is your updateTimer to be called every second, so just pass 1.0 to the scheduledTimer method, e.g.:
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
Furthermore, you want to invalidate this timer when 20 minutes have passed since starting the timer. So when setting up the timer you could keep track of the current time, and when that time + 20 minutes is more than the current time when called in updateTimer, you can invalidate the timer. With other words, instead of counting down from 20 minutes, we are counting from 0 til 20 minutes have passed!
Example code (didn't try compiling it, but should work, let me know if it doesn't):
import UIKit
class SkippingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var startWorkoutButton: UIButton!
#IBOutlet weak var pauseWorkoutButton: UIButton!
var timer = Timer()
var countDownFromMinutes = 20
var timerStartTime: Date?
override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "\(countDownFromMinutes):00"
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
}
#IBAction func startWorkoutButtonDidTouch(_ sender: Any) {
if !timer.isValid {
// run every second
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(SkippingViewController.updateTimer), userInfo: nil, repeats: true)
timerStartTime = nil
startWorkoutButton.isEnabled = false
pauseWorkoutButton.isEnabled = true
}
}
#IBAction func pauseWorkoutButtonDidTouch(_ sender: Any) {
startWorkoutButton.isEnabled = true
pauseWorkoutButton.isEnabled = false
timer.invalidate()
}
#objc func updateTimer() {
guard let startTime = timerStartTime else {
// first firing
timeLabel.text = "\(countDownFromMinutes):00"
timerStartTime = Date()
return
}
let now = Date()
let calendar = Calendar.current
// this ordinarily never returns nil, return gracefully if so
guard let endTime = calendar.date(byAdding: .minute, value: countDownFromMinutes, to: startTime) else {
return
}
let differenceMinuteSeconds = Calendar.current.dateComponents([.minute, .second], from: now, to: endTime)
// 20 minutes have passed since start
if now >= endTime {
timeLabel.text = "00:00"
timer.invalidate()
return
}
timeLabel.text = String(format: "%02d:%02d", differenceMinuteSeconds.minute ?? 0, differenceMinuteSeconds.seconds ?? 0)
}
}
Note that I also replaced the isRunning var you added with using timer.isValid as I believe it would achieve the same without introducing another variable.
Only thing that is probably left is regarding the text of label you are displaying. I am not sure what you actually want to display here. If you can add that in the comments I can suggest an approach for that.
You’ve set your timer’s interval to fire every 0.01 seconds, i.e. 100 times per second. And you’re adjusting the counter by 0.01 each time. So that means that it will decrement the counter at a rate of 1 per second. So a counter of 20 will expire in 20 seconds, not 20 minutes. If you want 20 minutes using your counter mechanism, you’d use 20 * 60.
A couple of other observations:
Timers are not guaranteed to fire at the requested interval. It’s safer to save the time to which you’re counting down (or the time you started) and calculate the time elapsed from that. Then you can update the label with a nice string representation of the time elapsed.
You’re firing your timer every hundredth of a second. But screens don’t generally update with that frequency. Besides, you’re just showing minutes and seconds, so updating more frequently than that offers no benefit.
If I were, though, showing milliseconds, and wanted an optimal timer frequency, I’d use a CADisplayLink rather than a timer. And, in the spirit of point 1, above, if you are using “start”/“stop” date to calculate how much time has elapsed, rather than a counter, you don’t have to worry about what this refresh rate is, since we’re no longer decrementing a counter.
I have created one timer object and set #selector method, In #selector method my label update every time that display timer count down value, but when I push or pop another view controller and come back to timer view controller my label not updating timer count down value
override func viewDidLoad() {
super.viewDidLoad()
if timer == nil {
self.startTimer()
}
}
func startTimer() {
timer?.invalidate()
timer = Timer.scheduledTimer(timeInterval:1, target: self, selector: #selector(self.update), userInfo: nil, repeats: true);
}
func update() {
count += 1
if(count > 0){
let ti = NSInteger(count)
let strSeconds = ti % 60
let strMinutes = (ti / 60) % 60
let strHours = (ti / 3600)
print("\(strHours):\(strMinutes):\(strSeconds)")
self.lblTimer.text = String(format: "%0.2d:%0.2d:%0.2d",strHours,strMinutes,strSeconds)
}
}
When you use the selector try self.update() to actually call the function. May also put a print statement to check that your variable is indeed incrementing.
I have a label and its value is "03:48".
I want to countdown it like a music player. How can I do that?
03:48 03:47 03:46 03:45 ... 00:00
var musictime =3:48
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60)
return String(format: "%02d:%02d", minutes, seconds)
}
func startTimer() {
var duration=musictime.componentsSeparatedByString(":") //split 3 and 48
var count = duration[0].toInt()! * 60 + duration[1].toInt()! //224 second
timerCounter = NSTimeInterval( count )
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
#objc func onTimer(timer:NSTimer!) {
// Here is the string containing the timer
// Update your label here
//println(stringFromTimeInterval(timerCounter))
statusLabel.text=stringFromTimeInterval(timerCounter)
timerCounter!--
}
You should take a look at NSDate property timeIntervalSinceNow. All you need is to set a future date as the endDate using NSDate method dateByAddingTimeInterval as follow:
class ViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
var remainingTime: NSTimeInterval = 0
var endDate: NSDate!
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
remainingTime = 228.0 // choose as many seconds as you want (total time)
endDate = NSDate().dateByAddingTimeInterval(remainingTime) // set your future end date by adding the time for your timer
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateLabel", userInfo: nil, repeats: true) // create a timer to update your label
}
func updateLabel() {
timerLabel.text = endDate.timeIntervalSinceNow.mmss
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// you will need this extension to convert your time interval to a time string
extension NSTimeInterval {
var mmss: String {
return self < 0 ? "00:00" : String(format:"%02d:%02d", Int((self/60.0)%60), Int(self % 60))
}
var hmmss: String {
return String(format:"%d:%02d:%02d", Int(self/3600.0), Int(self / 60.0 % 60), Int(self % 60))
}
}
Well, by writing code. This is not a "Teach me how to program site. It's a site where you post questions about specific problems you are having with code you have written.
In short, though do the following:
Record your end time
let endInterval = NSDate.timeIntervalSinceReferenceDate() + secondsToEnd
Create a repeating timer that fires once/second.
Each time it fires, compute the number of seconds remaining to endInterval.
Calculate minutes remaining as
Int((endInterval-nowInterval)/60)
Calculate seconds remaining as
Int(endInterval-nowInterval)%60
There is also the new (to iOS 8) class NSDateComponentsFormatter, which I've read a little about but haven't used. I believe that will generate formatted timer intervals like hh:mm:ss for you automatically. You'd use the same approach I outlined above, but instead of calculating minutes and seconds yourself, use the NSDateComponentsFormatter.
I'm trying to make a timer show the following - years : months : weeks : days : hours : minutes : seconds : milliseconds.
I don't want it to show the minutes until the seconds reaches 60, and the same for hours days etc. For example: If it's holding at 3 days 4 hours 39 min 3 seconds and 43 milliseconds, it shouldn't show the months or years because they would both be 0 so far.
Here is what I have.
#IBOutlet weak var timeLabel: UILabel!
var timerCount = 0
var timerRuning = false
var timer = NSTimer()
func counting() {
timerCount++
timeLabel.text = "\(timerCount)"
}
#IBAction func startTime(sender: UIButton)
{
if timerRuning == false {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("counting"), userInfo: nil, repeats: true)
timerRuning = true
}
}
#IBAction func finishTime(sender: UIButton)
{
if timerRuning == true {
timer.invalidate()
timerRuning = false
}
}
The problem is it only shows seconds. How can I get it to show all the rest? (Like I wrote above.)
There is a new class NSDateComponentsFormatter introduced in iOS 8.0 which can format an amount of seconds to readable time strings.
func counting() {
timerCount++
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
timeLabel.text = formatter.stringFromTimeInterval(NSTimeInterval(timerCount))
}
But there is one restriction: It doesn't consider units less than seconds.
I'm working on a timer app for iPhone. But when switching views and coming back to initial timer view,
the label is not updated. While I can see it's still running in the print log.
I have the code below in my viewDidLoad.
How can I start refreshing the label again when I enter the timer view again?
The other view is handled through Segue.
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 = minutes > 9 ? String(minutes):"0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)
println("----------")
println("currentTime")
println (currentTime)
println("elapsedTime")
println (elapsedTime)
println("extraTime")
println (extraTime)
println("summed")
println (summed)
//concatenate minuets, seconds and milliseconds as assign it to the UILabel
displayTimeLabel.text = "\(strMinutes):\(strSeconds)"
}
#IBAction func start(sender: AnyObject) {
if (!timer.valid) {
let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
startTime = NSDate.timeIntervalSinceReferenceDate()
}
}
#IBAction func stop(sender: AnyObject) {
timer.invalidate()
}
I was having the exact same problem in a tab view application and solved it using the NSNotification Center. To make it work in your case, you could make a separate function just for updating the text.
func updateText(notification: NSNotification) {
displayTimeLabel.text = "\(strMinutes):\(strSeconds)"
}
Then inside your "updateTime" function, where you had the line I took out, replace it with a postNotifiction:
NSNotificationCenter.defaultCenter().postNotificationName("UpdateTimer", object: nil)
Then put the observer inside a ViewDidAppear function in the View Controller where the text should be updated:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(false)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateText:", name: "UpdateTimer", object: nil)
}
With the observer in viewDidAppear, the updateText function always gets called, and the text is updated even when you switch views and come back.