I'm working on a chess GUI app (my first app) and I can't seem to figure out how to implement the drop part of drag and drop. So far I'm able to display a static chess board and pieces onto the screen and either use onTapGesture or DragGesture to move them but the UX isn't right yet. Do I need to use the draggable and dropDestination methods instead? Any advice or a simple code example that would get me going would be appreciated. Here are my requirements below to be clear:
- Display an 8x8 Grid View (or I suppose LazyGrid) where all cells contain a Color view, and some may overlay an SF Symbol Image depending on the models state
- UI needs to update the underlying model state and vice versa
- Drag and drop gesture should be self contained within the Grid View, so no transfer of data between other window instances
- Only one SF Symbol should be draggable and droppable at a time
- An SF symbol dropped onto another should remove it from the state model
- The drop gesture should snap a piece into the centre of the closest grid cell (or return it if it was an illegal chess move)
- No need for backwards compatibility, I'm focusing on current software and hardware
- Compatible with iOS, MacOS, iPadOS, WatchOS and potentially VisionOS
the 'drop' part of a DragGesture can be handled by the DragGesture.onEnded closure. In your .onChanged closure you can figure out where the drag is currently (value.location), so you could highlight legal destinations. In your .onEnded closure you figure out whether the move was legal and update your model accordingly.
Your question is a bit broad in scope. Try to narrow it down to something where you can show a piece of self-contained code, and explain what you expect, and what actually happens.