|
Does anyone know how to track laps in a stopwatch with SwiftUI? I already have this code:
But I have a start, pause, stop and laps button. When I click the laps button, the lap time should be saved in a list below the stopwatch. Does anyone know how to solve this? |
|
Without coding it up, I'd think you would use a Date object for your start time, and it would also assign it as the lapStartTime. When you want a lap split time, create a second Date object as the lapEndTime, do some date math w/ CalendarComponents and output the difference in whatever increment you are looking to use. Also, at this point the lapStartTime would be updated to use the lapEndTime as the end of one split is the start of the next. On a side note, do you really need to loop your timer so tightly (1/100th of a second)? That would seem to use a lot of cycles, when you could have it update maybe on the 1/10th of a second for showing the running display. When you actually select to stop, you would use that instant in time to create an endTimingDate and do similar date math as used for the lap split timer. Does this makes sense? |
|
I'm trying to build something similar for my Apple Watch. In my physical therapy I have so many sets of timed holds, 30 seconds each side for example then switch, over say 4 or 6 times. It's very difficult to get my Watch to inform me when the said time is up and I need continue without me struggling to see the watch face or restart a timer. In essence, I need an interval countdown timer, that also counts the laps. And haptic feedback is essential so my wrist vibrates and the clock resets and keeps counting. Learning SwiftUI, I am overwhelmed by so many tutorials obsession on fancy font color attributes and animations, those come intuitively to me. What is far more of my interest in learning as is challenging are the concepts introduced by State, Class, Struct, etc. etc... and that is just the beginning (ObservableObject oh my.) My intention is to keep building and rebuilding this App so the result is very similar (if not slight improved) but with the intention to understand SwiftUI better: build the same thing with more advanced tools- and do the aesthetic some other time. This is an ugly and currently working version. You choose one of the 4 options of start time, 15, 30, 45 or 60 seconds and the timer starts counting down and counts each loop. My goal is to introduce If anyone would like to help, the most urgent desire I have is to simplify the method that the actual timer counts down. I barely understand how onReceive works, in particular this part `// // HackingView.swift // InfiniteBuzz WatchKit Extension import SwiftUI struct HackingView: View { @State private var lapCount: Int = 0 @State private var startTime: CGFloat = 15 @State private var timeRemaining: CGFloat = 15 @State private var isActive = false
}` I can source the actual links if necessary but thanks to rebeloper, swiftful thinking and twostraws for the code copy and paste. |
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.