Hallo zusammen,
Mein Problem ist folgendes, Ich möchte sobald ich auf den Button drücke ein Rectangle gezeichnet wird ( das geht soweit ) jetzt möchte ich aber das durch erneutes drücken ein zweites Rectangle gezeichnet wird und das alte NICHT gelöscht wird.
Ich stehe irgendwie auf dem Schlau und komme einfach nicht auf die Lösung, ich hoffe jemand kann mir dabei helfen.
Alles anzeigen
Mein Problem ist folgendes, Ich möchte sobald ich auf den Button drücke ein Rectangle gezeichnet wird ( das geht soweit ) jetzt möchte ich aber das durch erneutes drücken ein zweites Rectangle gezeichnet wird und das alte NICHT gelöscht wird.
Ich stehe irgendwie auf dem Schlau und komme einfach nicht auf die Lösung, ich hoffe jemand kann mir dabei helfen.
Quellcode
- struct ContentView: View {
- @State private var isTapped = true
- @State var index = 0
- @State var scX = UIScreen.main.bounds.size.width
- @State var scY = UIScreen.main.bounds.size.height
- var body: some View {
- HStack {
- //Button
- Button(action: {
- self.isTapped.toggle()
- index += 1
- }) {
- Text("1")
- .padding()
- .foregroundColor(.black)
- .background(
- RoundedRectangle(cornerRadius: 10)
- .stroke(Color.black, lineWidth: 3)
- .frame(width: 75, height: 75)
- )
- }.position(CGPoint(x: 55, y: 395))
- // Add Rectangle
- HStack{
- if isTapped {
- RoundedRectangle(cornerRadius: 10, style: .continuous)
- .frame(width: 50, height: 50)
- .position(x: .random(in: 50...scX - 75), y: .random(in: 50...scY - 75))
- .foregroundColor(Color.red)
- }
- }
- }
- }