Hello,
on my stopwatch I want to compare a time in seconds and tenths recovered in a Picker with the last Lap made to change foregroundColor.
I got an error like "Type '()' cannot conform to 'View'" when I make this calcul :
comparaisonTemps = tempsTour <= lapTourCompare
Do you see the problem? Format of the tempsTour and lapTourCompare ? Thank you in advance for your help
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
@State var secondesSelection = 18
@State var dixiemesSelection = 0
@State var comparaisonTemps = false
@State var tempsTour = 0
@State var lapTourCompare = 0
let secondes = Array(10..<60)
let dixiemes = Array(0..<10)
var body: some View {
GeometryReader { geo in
HStack {
ZStack {
Rectangle()
.fill(Color.green.opacity(0.0))
.frame(width: UIScreen.main.bounds.width * 0.15)
VStack {
ZStack {
Rectangle()
.fill(Color.blue.opacity(0.0))
.frame(width: UIScreen.main.bounds.width * 0.15, height: UIScreen.main.bounds.height * 0.25)
VStack {
Text("Tps prévu au tour")
.font(.system(size: 20, design: .monospaced))
HStack {
Picker("Secondes", selection: $secondesSelection) {
ForEach(secondes, id: \.self) {
Text("\($0)''")
.font(.system(size: 20, design: .monospaced))
.fontWeight(.semibold)
}
}
Picker("Dixièmes", selection: $dixiemesSelection) {
ForEach(dixiemes, id: \.self) {
Text("\($0)")
.font(.system(size: 20, design: .monospaced))
.fontWeight(.semibold)
}
}
}
.pickerStyle(.wheel)
.frame(width: 140)
}
}
ZStack {
Rectangle()
.fill(Color.pink.opacity(0.0))
.frame(width: UIScreen.main.bounds.width * 0.15, height: UIScreen.main.bounds.height * 0.6)
VStack {
Text(getTimeString(m: self.minutes, s: self.seconds, c: self.centiseconds))
.font(.system(size: 20, design: .monospaced))
.onReceive(timer) {_ in
if(self.running) {
self.timerCalcs()
}
}
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)
}
}
Button(action: {
self.running = !self.running
})
{
ZStack {
Rectangle()
.fill(Color.indigo.opacity(0.0))
.frame(width: UIScreen.main.bounds.width * 0.15)
Circle().fill(self.running ? Color.red : Color.green).frame(height: 100).font(.system(size: 30, design: .monospaced))
self.running ? Text("Stop").foregroundColor(Color.white).font(.system(size: 30, design: .monospaced)) : Text("Start").foregroundColor(Color.white).font(.system(size: 30, design: .monospaced)) }
}
}
}
Unfortunately, I cannot test as we don't have complete code. We don't even know on which Rectangle onTapGesture applies.
Did you test what I proposed ?
.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
}
What do you get when doing this ?
.
How would I see the result returned by `let tempsTour = Double(secondesSelection) + Double(dixiemesSelection)
Where do you want it displayed ?