not sure how to fix error message from incorrectly converting string from text field input to double to be used in other part of code
also may have unwrapped optional wrong, not even sure my syntax is right when declaring it as an optional
thanks for your help!!!
import SwiftUI
struct ContentView: View {
// @State private var xRadius = 150 // default values for optional
// @State private var yRadius = 75 // default values for optional
//
// @State private var: Double(xRadius2) = 0
// @State private var: Double(yRadius2) = 0
@State private var xRadiusInput: String? = " "
@State private var yRadiusInput: String? = " "
var xRadius: CGFloat
{
CGFloat(Double?(xRadiusInput) ?? 150)
}
var yRadius: CGFloat
{
CGFloat(Double?(yRadiusInput) ?? 75)
}
var speed: CGFloat = 0.5
var body: some View {
TimelineView(.animation) { timeline in
var speed = (xRadius + yRadius) / 100
let time = timeline.date.timeIntervalSinceReferenceDate
let angle = time * speed * 2 * .pi
let x = cos(angle) * xRadius //angle is where you are on the circle, cos and sin are the x
let y = sin(angle) * yRadius
Text("X dimension: , \(xRadius)")
TextField("Enter x dimension of elliptical orbit", text: $xRadiusInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text("Y dimension: \(yRadius)")
TextField("Enter y dimension of elliptical orbit", text: $yRadiusInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
ZStack {
Ellipse()
.stroke(Color.gray.opacity(0.3), lineWidth: 2)
.frame(width: xRadius * 2, height: yRadius * 2)
// .offset(x: 50, y: 3)
Circle()
.fill(Color.blue)
.frame(width: 30, height: 30)
.offset(x: x, y: y) // x / y as in lines 22 and 23
Circle()
.fill(Color.red)
.frame(width: 75, height: 75)
}
.frame(width: xRadius * 2 + 60, height: yRadius * 2 + 60)
}
}
}
#Preview {
ContentView()
}