Post

Replies

Boosts

Views

Activity

SwiftUI: How to create a multiple timer app with a list of timers and how to jump between the timer views
I just created a timer app for me and my friends with multiple timers. To navigate between these timers i created a list for each timer in an array of timerObjects instanzes. struct ContentView: View { private let timers = [TimerObject(name: "Kuchen", duration: 15), TimerObject(name: "Atem anhalten", duration: 30), TimerObject(name: "Abflug", duration: 7200)] var body: some View { NavigationView { List(timers, id: \.id) { timer in NavigationLink { TimerView(timerObject: timer) } label: { Text(timer.name) } } } } } The TimerView() is just the view that handels and present the whole thing behind the timer. My Problem now is now: if i start a timer in the TimerView() and than go back to the list and than go back to the started timer, i get a new instanz of my timer and the TimerView() don´t show me the started timer. My goal is to go back to the started timer! To clarify my problem, I also recorded my problem once and uploaded it to youtube so that you can see it too: https://youtu.be/bdh-122-Jx0 I would be very happy about help. Best regards!
0
0
699
Jan ’22
SwiftUI: How to go back to the previous instance in a navigationlink in a list
Hi. I would like to code a multiple timer app for me and my family. And i would now like to show these multiple timers in a list. From the list the user can tap on a timer (list row) and than go to the selected timer. Like in this app: https://youtu.be/KAIOYlXi2KE So far I've already managed to generate the transition from my list to my timer view with a navigation link in my list:                 NavigationLink {                     TimerView(timerObject: timer)                 } label: {                     Text(timer.name)                 }             } But if I now go back to my list, and then try to go to the same timer which I started earlier, a completely new instance of my timer view is created even though I am still getting the print commands (every second) in my console of my earlier started timer. So how can I code that each list row has only a single instance of a timer and then always goes back to just that one timer? I would be really happy about help. Thank you!
1
0
551
Jan ’22
SwiftUI: Edit list row relement with swipe action and present modal sheet
Hi. I have a swiftui project with a list. I would now like to add 2 trailing swipe actions to this list, the .onDelete swipe action and a edit swipe action to the left of it. Like this: To achieve this in swiftui I added the following code to my list: List { ForEach(timers, id: \.id) { timer in TimerRow(timer: timer) } .onDelete(perform: { IndexSet in deleteTimer(IndexSet) }) .swipeActions(edge: .trailing, allowsFullSwipe: false) { Button { // Open edit sheet isShowEditTimer.toggle() } label: { Image(systemName: "pencil.circle") } } } But unfortunately only the edit function is displayed now: Do you know how I can solve my problem? But now to my real problem: I now want to open a modal sheet when the edit swipe action of a line is pressed. But how do I find out which line was swiped on? With the .onDelete function we get an IndexSet, but nothing here I would also like to give the struct that is called in my sheet this certain swiped element (CoreData object): .sheet(isPresented: $isShowEditTimer) { EditTimerView(timerObject: ???) } By the way, this sheet is applied to my navigation view. I would be really happy if someone could help me and if you didn't report my post. Maybe this question has been asked somewhere deep in StackOverflow, but I'm also relatively new to swiftui (always UIKit before) and don't understand every stackoverflow post yet. Thanks!!! 😀
0
0
839
Jan ’22
SwiftUI How to pass a CoreData object to an other view
Hey I just added another view to my project and now I would like to put a CoreData instance of type "Timer" in here: struct TimerRow: View { let timer: Timer var body: some View { HStack { VStack(alignment: .leading) { Text("Timer") } } } } struct TimerRow_Previews: PreviewProvider { static var previews: some View { TimerRow() } } But now I get an error in my TimerRow_Previews. It's clear, the PreviewProvider doesn't know what to hand over to the initializer. Only what do I hand over to the PreviewProvider now? Is there something like .constant() test values only for CoreData instances? I would be very happy about an answer. Warm greetings
1
0
578
Jan ’22
Swift how to create an accurate timer app
Hey I've been trying to create an accurate timer in Swift for a while now. I started with timer.shedueld to call up a closure every second, which then counts down a counting variable. But then I was told very often that this type of timer was not accurate and that I should try Date (). So my code looks like this: @objc func startStopButton_Tapped() { if startStopButton.titleLabel?.text == "Start" { // Start timer endTime = Date(timeInterval: TimeInterval(remainingSeconds), since: .now) timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true) RunLoop.main.add(timer, forMode: .common) startStopButton.setTitle("Stopp", for: .normal) } else { // Stop timer timer.invalidate() startStopButton.setTitle("Start", for: .normal) } } @objc func resetButton_Tapped() { } @objc func updateTime() { if endTime > Date.now { timerLabel.text = formatter.string(from: Date.now, to: endTime) remainingSeconds = Int(endTime.timeIntervalSinceNow) } else { // Timer ended timerLabel.text = formatter.string(from: TimeInterval(0)) timer.invalidate() print("fertig") } } But unfortunately it doesn't work well. For example, I want it to count down from 60 seconds, but when I start the timer it suddenly jumps to 58 instead of 59. So has someone already code that I can look at? Or who has suggestions for improvement? Warm greetings
0
0
850
Dec ’21
Swift dynamic uilabel size
Hey I am desperate how I can make my app looks good for all end devices. I have e.g. a completely normal label with the text "Start" and the size UIFont (name: "Arial", size: 35). It also looks good on an Iphone 12 pro max, but the text on an iphone se is simply way too big. How do I get it to look good? And why can't apple adjust the size itself? I would be very happy to hear from you. Best regards
1
0
3.2k
Dec ’21
Swift corner radius proportions for all device types.
Hey. Hi. I assigned a corner radius of 30 to my uibutton with this code:         startButton.layer.cornerRadius = 30 So far it looks very good on an iphone 13 pro max, but only there. So my question is: How can I create a corner radius that has the same proportions on all iphones? So looks good everywhere? (My button changes heigh depending on the heigh of the user's device:) stackView1.heightAnchor.constraint(equalToConstant: height * 0.065).isActive = true Can somebody help me with it? Greetings
1
0
754
Dec ’21
Use volume buttons observer by pressing it without violating the app store guidelines
Hi. I would like to add an observer to my volume buttons to start a timer when the volume buttons are pressed (like a real stopwatch). That works fine but I will have to disable the volume buttons so that the user does not change his system volume while he starts the timer by pressing a button. Unfortunately this is not allowed according to the Apple AppStore guidelines no. 2.5.9 from December 8th 2021 https://developer.apple.com/app-store/review/guidelines/#hardware-compatibility 2.5.9 Apps that alter or disable the functions of standard switches, such as the Volume Up/Down and Ring/Silent switches, or other native user interface elements or behaviors will be rejected. For example, apps should not block links out to other apps or other features that users would expect to work a certain way. Learn more about proper handling of links. So does anyone have another idea how I can implement my desired function without violating the guidelines? I'd be happy for help. Best regards 😀
1
0
1.6k
Dec ’21