UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I select multiple views?

Forums > SwiftUI

My app has multiple images that I want to move around. Then I would like to perform a region pick on several views and move to a different NavigationLink. Not sure how to do a region pick.

Thanks, Kelly

2      

More on the context, I am working on a card game and need to select say 3 of the cards in a hand as a meld/group. Once selected, I will remove them from the hand and put into a table meld area where the other players can see them. So, trying to think of a way to do a region select across the cards, have them highlighted for verification then move on to the meld area.

Thanks, Kelly

2      

I think it would be more helpful to approach the problem as how to select an individual card object rather than how to select the view displaying that card object.

Your Card class should have an id property to identify each card instance. When the user selects a card, you could add its id to an @State var that is an array storing the id's of the selected cards. (Alternatively, you could include a "selected" boolean property in the Card class.)

If you have the patience to work through it, an elaborate example is the card game designed in the first 8 lectures of the Stanford course on SwiftUI: https://cs193p.sites.stanford.edu/

2      

Thanks for your advice, I will look at the course for this and other ideas and use the 'id' / Bool method for selection. I am just surprised that I can't drag my curser over a region and have that select cards in that region. I am using 3 taps to discard and now a long press to select for meld.

2      

Correction: Card class should be Card struct.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.