Vegard
November 22, 2019, 10:24am
1
Hi! I am trying to make an app where you can start a timer when you start at work and stop the timer when you´re done at work and then it makes a list that you can export and it will say what days and times you have been working that month. So far I am having trouble with the timer. I want it to have this format 00.00 so if you have worked for 2 hours and 34 minutes it says 02.34/2.34. But it keeps going to 100 minutes before it will add a number to the “hour” position. Any ideas on how I could do this?
Here is the code I have so far:
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var timecounter: UILabel!
var counter = 0.0
var timer = Timer()
var isPlaying = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
timecounter.text = String(counter)
}
@IBAction func StartDay(_ sender: Any) {
if(isPlaying) {
return
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(UpdateTimer), userInfo: nil, repeats: true)
isPlaying = true
}
@IBAction func EndDay(_ sender: Any) {
timer.invalidate()
isPlaying = false
counter = 0.0
timecounter.text = String(counter)
}
@objc func UpdateTimer() {
counter = counter + 0.01
timecounter.text = String(format: "%.2f", counter)
if (counter == 0.10){
counter = 1.00
counter = counter + 0.01
timecounter.text = String (format: "%.2f", counter)
}
}
}
Thanks
Vegard:
@objc func UpdateTimer() {
counter = counter + 0.01
timecounter.text = String(format: “%.2f”, counter)
if (counter == 0.10){
counter = 1.00
counter = counter + 0.01
timecounter.text = String (format: “%.2f”, counter) }
what is this even?.. why 0.01?
you are not using correct “time” calculations/logic if thats the case
Vegard
November 27, 2019, 11:47am
3
Thanks for your replay. I figured out a solution were I just added a second timer for the hour. So if any one else is wondering what I did here´s my code as for now:
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var timecounter: UILabel!
@IBOutlet weak var hourcounter: UILabel!
var counter = 0
var counterh = 0
var timer = Timer()
var timerh = Timer()
var isPlaying = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
timecounter.text = String(format: "%02d", counter)
hourcounter.text = String(format: "%02d", counterh)
}
@IBAction func StartDay(_ sender: Any) {
if(isPlaying) {
return
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(UpdateTimer), userInfo: nil, repeats: true)
timerh = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(UpdateTimer2), userInfo: nil, repeats: true)
isPlaying = true
}
@IBAction func EndDay(_ sender: Any) {
timer.invalidate()
timerh.invalidate()
isPlaying = false
counter = 0
counterh = 0
timecounter.text = String(format: "%02d", counter)
hourcounter.text = String(format: "%02d", counterh)
}
@objc func UpdateTimer() {
counter = counter + 1
timecounter.text = String(format: "%02d", counter)
if (counter == 59) {
counter = 0
}
}
@objc func UpdateTimer2(){
counterh = counterh + 1
hourcounter.text = String(format: "%02d", counterh)
}
}