Post

Replies

Boosts

Views

Activity

Reply to Calculation for a Picker
Hello, I come back for a problem encountered on another View. The result of my calculation is not exact because I think that the value retrieved in the Picker does not include tenths, but above all the variation of the Slider TpsFirstTour has no effect on the result. Do you see where it comes from? Thanks in advance     @State var minutesSelection = 18     @State var secondesSelection = 0     @State private var valeurSliderNbreTour = 14.0     @State private var valeurSliderTpsFirstTour = 20.0          let minutes = Array(1..<6)     let secondes = Array(0..<60)          // Creation de la variable tps au tour     var tpsauTour: Double {         let tpspoursuite = Double(minutesSelection) + (Double(secondesSelection))         return (((Double(tpspoursuite) * 3600) - valeurSliderTpsFirstTour )/(valeurSliderNbreTour - 1))     }     // Format du tps au tour     var formattedtpsauTour: String {         "\(tpsauTour.formatted(.number.precision(.significantDigits(3)))) s"     }          var body: some View {         VStack {             Text("Temps total de la poursuite")                 .font(.title.bold())                 .foregroundColor(.blue)             HStack( spacing : 120.0) {                                 Text("Minutes")                 Text("Secondes")                           }             HStack {                 Picker("Minutes", selection: $minutesSelection) {                     ForEach(minutes, id: \.self) {                         Text("\($0)'")                     }                 }                 Picker("Secondes", selection: $secondesSelection) {                     ForEach(secondes, id: \.self) {                         Text("\($0)")                     }                 }             }             .pickerStyle(.wheel)                          Text("Temps du premier tour")                 .font(.title.bold())                 .foregroundColor(.blue)                          Slider(value: $valeurSliderTpsFirstTour, in: 15...25, step: 0.1) {             } minimumValueLabel: {                 Text("15.0")             } maximumValueLabel: {                 Text("25.0")             }             Text(valeurSliderTpsFirstTour.description)                 .font(.largeTitle)                          Text("Nombre de tours")                 .font(.title.bold())                 .foregroundColor(.blue)                          Slider(value: $valeurSliderNbreTour, in: 8...20, step: 1.0) {             } minimumValueLabel: {                 Text("8")             } maximumValueLabel: {                 Text("20")             }             Text(Int(valeurSliderNbreTour).description)                 .font(.largeTitle)                          Text("Temps au tour: \(formattedtpsauTour)")                       .font(.title.bold())                       .foregroundColor(.blue)             }                  }     }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Calculation for a Picker
I found the right formula for my problem ;) `var tpsauTour: Double {         let tpspoursuite = (Double(minutesSelection)*60) + Double(secondesSelection)         return ((Double(tpspoursuite)-valeurSliderTpsFirstTour) / (valeurSliderNbreTour - 1))     }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’22
Reply to Problem with GeometryReader
Hi, it's the iPad Pro 12,9 pouces. After change I have that : the code if you want test Thanks let timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect() @State var minutes = 0 @State var seconds = 0 @State var centiseconds = 0 @State var running = false @State var lapTimes: [LapTime] = [] @State var lapCount = 1 @State var lapMinutes = 0 @State var lapSeconds = 0 @State var lapCentiseconds = 0 var body: some View { GeometryReader { geo in VStack(spacing: 0){ Text(getTimeString(m: self.minutes, s: self.seconds, c: self.centiseconds)) .font(.system(size: min(geo.size.height, geo.size.width) * 0.05, design: .monospaced)) .frame(height: UIScreen.main.bounds.height * 0.05) // <<-- HERE .onReceive(timer) {_ in if(self.running) { self.timerCalcs() } } Text("\(lapCount - 1)") .font(.system(size: min(geo.size.height, geo.size.width) * 0.25)) .fontWeight(.bold) .frame(height: UIScreen.main.bounds.height * 0.25) // <<-- HERE .foregroundColor(.red) ZStack { Text(self.lapTimes.last?.getLapSecondsString() ?? "") .font(.system(size: min(geo.size.height, geo.size.width) * 0.45)) .fontWeight(.bold) .frame(height: UIScreen.main.bounds.height * 0.38) // <<-- HERE Rectangle().fill(Color.gray.opacity(0.1)) } HStack(spacing : 10) { Button(action: { if(!self.running) { self.minutes = 0 self.seconds = 0 self.centiseconds = 0 self.lapTimes = [] self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 self.lapCount = 1 } else { self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)) self.lapCount += 1 self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 } }) { ZStack{ Circle().fill(Color.gray).frame(height: UIScreen.main.bounds.height * 0.12) // <<-- HERE self.running ? Text("Lap").foregroundColor(Color.white).font(.system(size: min(geo.size.height, geo.size.width) * 0.04, design: .monospaced)) : Text("Reset").foregroundColor(Color.white).font(.system(size: min(geo.size.height, geo.size.width) * 0.04, design: .monospaced)) } } .padding(8) Spacer() Button(action: { self.running = !self.running }) { ZStack{ Circle().fill(self.running ? Color.red : Color.green).frame(height: UIScreen.main.bounds.height * 0.12).font(.system(size: min(geo.size.height, geo.size.width) * 0.04, design: .monospaced)) // <<-- HERE self.running ? Text("Stop").foregroundColor(Color.white).font(.system(size: min(geo.size.height, geo.size.width) * 0.04, design: .monospaced)) : Text("Start").foregroundColor(Color.white).font(.system(size: min(geo.size.height, geo.size.width) * 0.04, design: .monospaced)) } } .padding(8) } List { LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds) ForEach(self.lapTimes.reversed()) { time in time } } } } } func timerCalcs() { if(self.centiseconds < 99) { self.centiseconds += 1 } else { self.centiseconds = 0 if(self.seconds < 59) { self.seconds += 1 } else { self.seconds = 0 self.minutes += 1 } } if(self.lapCentiseconds < 99) { self.lapCentiseconds += 1 } else { self.lapCentiseconds = 0 if(self.lapSeconds < 59) { self.lapSeconds += 1 } else { self.lapSeconds = 0 self.lapMinutes += 1 } } } } func getTimeString(m: Int, s: Int, c: Int) -> String { var centiString = String(c) var secString = String(s) var minString = String(m) if(c < 10) { centiString = "0\(c)" } if(s < 10) { secString = "0\(s)" } if(m < 10) { minString = "0\(m)" } return "\(minString):\(secString).\(centiString)" } struct LapTime: View, Identifiable { let id = UUID() let num: Int let minutes: Int let seconds: Int let centiSeconds: Int let time: String var body: some View { HStack { Text("Lap \(num)").font(.system(size: 20, design: .monospaced)) Spacer() Text(time).font(.system(size: 20, design: .monospaced)) } } init(n: Int, m: Int, s: Int, c: Int) { num = n minutes = m seconds = s centiSeconds = c time = getTimeString(m: minutes, s: seconds, c: centiSeconds) } func getLapSecondsString() -> String { var centiString = String(centiSeconds) var secString = String(seconds) if(centiSeconds < 10) { centiString = "0\(centiSeconds)" } if(seconds < 10) { secString = "0\(seconds)" } return "\(secString).\(centiString)" } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Color of BackGround and Condition
Hello, I'm coming back to you because I have a problem comparing my lap time (retrieved via the Picker) and the lapTimes.last the value of the lap time is let tempsTour = Double(secondesSelection) + (Double(dixiemesSelection) / 10) but I don't see how to retrieve the LapTimes.last in the same format to perform the comparison. a idea? Thanks code snippet List { LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds) ForEach(self.lapTimes.reversed()) { time in time } }.frame(width: UIScreen.main.bounds.width * 0.15) } ZStack { Rectangle() .fill(Color.yellow.opacity(0.0)) .frame(height: UIScreen.main.bounds.height * 0.65) Text(self.lapTimes.last?.getLapSecondsString() ?? "") .font(.system(size: min(geo.size.height, geo.size.width) * 0.40)) .fontWeight(.bold) .frame(height: UIScreen.main.bounds.height * 0.38) // <<-- HERE } } Rectangle() .fill(Color.green.opacity(0.1)) .onTapGesture { if(!self.running) { self.minutes = 0 self.seconds = 0 self.centiseconds = 0 self.lapTimes = [] self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 self.lapCount = 1 } else { self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)) self.lapCount += 1 self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 } } } } } } func timerCalcs() { if(self.centiseconds < 99) { self.centiseconds += 1 } else { self.centiseconds = 0 if(self.seconds < 59) { self.seconds += 1 } else { self.seconds = 0 self.minutes += 1 } } if(self.lapCentiseconds < 99) { self.lapCentiseconds += 1 } else { self.lapCentiseconds = 0 if(self.lapSeconds < 59) { self.lapSeconds += 1 } else { self.lapSeconds = 0 self.lapMinutes += 1 } } } } func getTimeString(m: Int, s: Int, c: Int) -> String { var centiString = String(c) var secString = String(s) var minString = String(m) if(c < 10) { centiString = "0\(c)" } if(s < 10) { secString = "0\(s)" } if(m < 10) { minString = "0\(m)" } return "\(minString):\(secString).\(centiString)" } struct LapTime: View, Identifiable { let id = UUID() let num: Int let minutes: Int let seconds: Int let centiSeconds: Int let time: String var body: some View { HStack { Text("\(num)").font(.system(size: 20, design: .monospaced)) //Lap Spacer() Text(time).font(.system(size: 20, design: .monospaced)) } } init(n: Int, m: Int, s: Int, c: Int) { num = n minutes = m seconds = s centiSeconds = c time = getTimeString(m: minutes, s: seconds, c: centiSeconds) } func getLapSecondsString() -> String { var centiString = String(centiSeconds) var secString = String(seconds) if(centiSeconds < 10) { centiString = "0\(centiSeconds)" } if(seconds < 10) { secString = "0\(seconds)" } return "\(secString).\(centiString)" } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Display of the last LAP
hello, how to retrieve the value of the lap Times.last to compare it with the lap time retrieved via the Picker? I think the picker format for tempsTour is let tempsTour = Double(secondesSelection) + (Double(dixiemesSelection) / 10) Thanks
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Compare 2 values
ZStack { Rectangle() .foregroundColor(comparaisonTemps ? Color.red.opacity(0.5) : Color.green.opacity(0.5)) .onTapGesture { if(!self.running) { self.minutes = 0 self.seconds = 0 self.centiseconds = 0 self.lapTimes = [] self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 self.lapCount = 1 } else { self.lapTimes.append(LapTime(n: self.lapCount, m: self.lapMinutes, s: self.lapSeconds, c: self.lapCentiseconds)) self.lapCount += 1 self.lapMinutes = 0 self.lapSeconds = 0 self.lapCentiseconds = 0 } } let tempsTour = Double(secondesSelection) + Double(dixiemesSelection) let lapTourCompare = Double(seconds) + Double(centiseconds) // comparaisonTemps = tempsTour <= lapTourCompare VStack { ZStack { Rectangle() .fill(Color.brown.opacity(0.0)) .frame(height: UIScreen.main.bounds.height * 0.3) Text("\(lapCount - 1)") .font(.system(size: min(geo.size.height, geo.size.width) * 0.40)) .fontWeight(.bold) .frame(height: UIScreen.main.bounds.height * 0.25) .foregroundColor(.red) } ZStack { Rectangle() .fill(Color.yellow.opacity(0.0)) .frame(height: UIScreen.main.bounds.height * 0.65) Text(self.lapTimes.last?.getLapSecondsString() ?? "") .font(.system(size: min(geo.size.height, geo.size.width) * 0.40)) .fontWeight(.bold) .frame(height: UIScreen.main.bounds.height * 0.38) } } } } } } func timerCalcs() { if(self.centiseconds < 99) { self.centiseconds += 1 } else { self.centiseconds = 0 if(self.seconds < 59) { self.seconds += 1 } else { self.seconds = 0 self.minutes += 1 } } if(self.lapCentiseconds < 99) { self.lapCentiseconds += 1 } else { self.lapCentiseconds = 0 if(self.lapSeconds < 59) { self.lapSeconds += 1 } else { self.lapSeconds = 0 self.lapMinutes += 1 } } } } func getTimeString(m: Int, s: Int, c: Int) -> String { var centiString = String(c) var secString = String(s) var minString = String(m) if(c < 10) { centiString = "0\(c)" } if(s < 10) { secString = "0\(s)" } if(m < 10) { minString = "0\(m)" } return "\(minString):\(secString).\(centiString)" } struct LapTime: View, Identifiable { let id = UUID() let num: Int let minutes: Int let seconds: Int let centiSeconds: Int let time: String var body: some View { HStack { Text("\(num)").font(.system(size: 20, design: .monospaced)) //Lap Spacer() Text(time).font(.system(size: 20, design: .monospaced)) } } init(n: Int, m: Int, s: Int, c: Int) { num = n minutes = m seconds = s centiSeconds = c time = getTimeString(m: minutes, s: seconds, c: centiSeconds) } func getLapSecondsString() -> String { var centiString = String(centiSeconds) var secString = String(seconds) if(centiSeconds < 10) { centiString = "0\(centiSeconds)" } if(seconds < 10) { secString = "0\(seconds)" } return "\(secString).\(centiString)" } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22
Reply to Compare 2 values
Thanks for your help. No problem with the picker. it recovers seconds and tenths. Example 15"8 would be a lap time to aim for for the athlete and I have to compare it with his last Lap. If his Lap is higher the Rectangle() is red otherwise it is green. The declarations are already in the Zstack, how should I to do with the calcul comparisonTemps? How would I see the result returned by `let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)                     let lapTourCompare = Double(seconds) + Double(centiseconds)`
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22