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)) }
}
}
}
Post
Replies
Boosts
Views
Activity
Hi,
I simply want to compare 2 values to determine the color of the background.
I am unable to handle the if condition properly.
If "tpsRealise" > "tpsEstime" background is red else is green.
Thanks
struct ContentView: View {
@State var comparaisonTemps = true
@State var tpsRealise = 0
@State var tpsEstime = 0
var backgroundColor : Color {
return comparaisonTemps ? Color.green : Color.red
}
var body: some View {
ZStack {
backgroundColor
VStack {
Text("Tps Réalisé : \(tpsRealise)")
Image(systemName: "plus.square")
.font(.title)
.onTapGesture {
tpsRealise += 1
}
Spacer()
Text("Temps estimé : \(tpsEstime)")
Image(systemName: "plus.square")
.font(.title)
.onTapGesture {
tpsEstime += 1
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Hello,
I used GeometryReader to proportion the size of my Frames vertically regardless of the device. Yet the List on the Ipad contains 3 lines and only 1 on the Iphone.
Where is my error in the code for this layout?
Thanks in advance
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)
.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)
.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)
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)
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))
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
}
}
}
}
}
Hello,
I use a timer shared on GitHub. I want to display the last LAP recorded under the stopwatch (see screenshot). I manage to recover the LAP but not the LapTime.
Thank you in advance for your assistance.
struct ContentView: View {
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 {
VStack{
Text(getTimeString(cS: self.lapCentiseconds, sS: self.lapSeconds, mS: self.lapMinutes))
.font(.system(size: 60, design: .monospaced))
.frame(width: 300.0, height: 100.0)
.onReceive(timer){_ in
if(self.running){
self.timerCalcs()
}
}
Text(getTimeString(cS: self.centiseconds, sS: self.seconds, mS: self.minutes))
.font(.system(size: 60, design: .monospaced))
.frame(width: 300.0, height: 100.0)
.onReceive(timer){_ in
if(self.running){
self.timerCalcs()
}
}
Text("Lap \(lapCount - 1)")
// Text(time)
HStack{
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, t: self.getTimeString(cS: self.lapCentiseconds, sS: self.lapSeconds, mS: self.lapMinutes)))
self.lapCount += 1
self.lapMinutes = 0
self.lapSeconds = 0
self.lapCentiseconds = 0
}
}) {
ZStack{
Circle().fill(Color.gray).frame(width: 100, height: 100)
self.running ? Text("Lap").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) : Text("Reset").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced))
}
}
Spacer()
Button(action: {
self.running = !self.running
}) {
ZStack{
Circle().fill(self.running ? Color.red : Color.green).frame(width: 100, height: 100).font(.system(size: 20, design: .monospaced))
self.running ? Text("Stop").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced)) : Text("Start").foregroundColor(Color.white).font(.system(size: 20, design: .monospaced))
}
}
}.padding()
List{
LapTime(n: self.lapCount, t: self.getTimeString(cS: self.lapCentiseconds, sS: self.lapSeconds, mS: self.lapMinutes))
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(cS: Int, sS : Int, mS: Int) -> String{
var centiString = String(cS)
var secString = String(sS)
var minString = String(mS)
if(cS<10){
centiString = "0\(cS)"
}
if(sS<10){
secString = "0\(sS)"
}
if(mS<10){
minString = "0\(mS)"
}
return "\(minString):\(secString).\(centiString)"
}
}
struct LapTime : View, Identifiable{
let id = UUID()
let num : 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, t : String){
num = n
time = t
}
}
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()
}
}