|
I am working on a card game where a hand starts with 10 cards, could go up dramatically, but also go down to 0 cards. I would like to be able to show each card and drag them around individually to group them. I can do this if I hard code say the 10 starting card, but thought I would try ForEach to then show however many is in the count, but so far the cards are group and all drag together. Thanks! This is the hardcoded version that allows me to drag individual images: HStack { VStack{Image(playernames.showcards[0]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card1pos.x, y: card1pos.y).gesture(DragGesture().onChanged { value in card1pos = value.location})} VStack{Image(playernames.showcards[1]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card2pos.x, y: card2pos.y).gesture(DragGesture().onChanged { value in card2pos = value.location})} VStack{Image(playernames.showcards[2]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card3pos.x, y: card3pos.y).gesture(DragGesture().onChanged { value in card3pos = value.location})} VStack{Image(playernames.showcards[3]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card4pos.x, y: card4pos.y).gesture(DragGesture().onChanged { value in card4pos = value.location})} VStack{Image(playernames.showcards[4]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card5pos.x, y: card5pos.y).gesture(DragGesture().onChanged { value in card5pos = value.location})} VStack{Image(playernames.showcards[5]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card6pos.x, y: card6pos.y).gesture(DragGesture().onChanged { value in card6pos = value.location})} VStack{Image(playernames.showcards[6]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card7pos.x, y: card7pos.y).gesture(DragGesture().onChanged { value in card7pos = value.location})} VStack{Image(playernames.showcards[7]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card8pos.x, y: card8pos.y).gesture(DragGesture().onChanged { value in card8pos = value.location})} VStack{Image(playernames.showcards[8]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card9pos.x, y: card9pos.y).gesture(DragGesture().onChanged { value in card9pos = value.location})} VStack{Image(playernames.showcards[9]).resizable().scaledToFit().frame(width: 40, height: 60).position(x: card10pos.x, y: card10pos.y).gesture(DragGesture().onChanged { value in card10pos = value.location})} }
struct ContentView: View { @State private var card1pos: CGPoint = CGPoint(x: 30, y: 40) var mycards: [String] = ["2_of_spades", "3_of_diamonds", "4_of_spades","joker_red"] var body: some View {
} |
|
Hi, you could create a Card View so that each card holds it's own position, i used text for the example but you could change it to images
then in ContentView use a forEach with the number of cards you want:
Hope it's what you're looking for. |
|
|
|
This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such. Doing this will ensure that you end up with something like this:
instead of this: func printSomething(_ thing: String?) { if let thing = thing { print(thing) } else { print("nothing there") } } |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.