Hello, I'm new to SwiftUI programming. I am looking to do a calculation from 3 Picker (wheel). In the first I have to retrieve a track length in meters. In the second and the third I recover 2 values which associated make seconds and tenths (lap time). The calculation I want to perform = (length / 1000) / ( lap time (in seconds and tenths) / 3600)
thank you in advance for your help
// Created by Thierry Lebeau on 24/11/2022.
//
import SwiftUI
struct ContentView: View {
@State var secondeSelection = 0
@State var dixiemeSelection = 0
@State var pisteSelection = 0
var secondes = [Int](10..<60)
var dixiemes = [Int](0..<10)
let longueurPiste = [200, 250, 333, 500]
var vitesseCalculee = Double()
var body: some View {
VStack {
Label("Calcul du temps au tour", systemImage: "stopwatch")
Spacer()
Text("Longueur de la piste")
.font(.title)
.fontWeight(.semibold)
.foregroundColor(Color.blue)
Picker(selection: self.$pisteSelection, label: Text("")){
ForEach(0 ..< longueurPiste.count){ index in
Text("\(self.longueurPiste[index]) m").tag(index)
}
}
.pickerStyle(WheelPickerStyle())
// Spacer()
Text("Temps au tour")
.font(.title)
.fontWeight(.semibold)
.foregroundColor(Color.blue)
HStack{
Text("Secondes")
Text("Dixièmes")
}
HStack {
Picker(selection: self.$secondeSelection, label: Text("")){
ForEach(0 ..< self.secondes.count){ index in
Text("\(self.secondes[index])'").tag(index)
}
}
.pickerStyle(WheelPickerStyle())
Picker(selection: self.$dixiemeSelection, label: Text("")){
ForEach(0 ..< self.dixiemes.count){ index in
Text("\(self.dixiemes[index])").tag(index)
}
}
.pickerStyle(WheelPickerStyle())
}
Spacer()
Text("Vitesse calculée: \(secondes[secondeSelection])'\(dixiemes[dixiemeSelection])")
.font(.largeTitle)
.fontWeight(.regular)
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
}
Spacer()
// .background(.cyan)
}
}
// vitesseCalculee = (longueurPiste/1000) / (tempsTour/3600)
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
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)
}
}
}