ios – Faucet wherever so as to add a number of objects on canvas

[ad_1]

You solely have a singular location, so just one merchandise will ever seem in your instance. To have a number of objects, you will want some kind of assortment, like an Array.

The next is a pared-down instance exhibiting utilizing an array:

struct Hammer: Identifiable {
    var id = UUID()
    var location: CGPoint
}

struct Level1: View {
    @State var hammers: [Hammer] = [] //<-- Begin with `none`
    
    var physique: some View {
        ZStack {
            ForEach(hammers) { hammer in // Show the entire hammers
                Picture(systemName: "hammer.fill")
                    .resizable()
                    .body(width: 30, top: 30)
                    .place(hammer.location)
            }
        }
        .body(maxWidth: .infinity, maxHeight: .infinity)
        .contentShape(Rectangle())
        .gesture(DragGesture(minimumDistance: 0).onEnded { worth in
            self.hammers.append(Hammer(location: worth.location)) // Add a Hammer
        })
        .edgesIgnoringSafeArea(.all)
    }
}

Word: I am unclear on what the GeometryReader is for in your code — you declare it, then use UIScreen dimensions — usually in SwiftUI we simply use GeometryReader

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *