SwiftUI Live: Building a drag and drop word game

Paul Hudson · 23K views · 2019 · 2:08:17

Open on YouTube

Software (other)Game programming & enginestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as a worked example of tracking view frames with GeometryReader overlays and handling drag and drop gestures in SwiftUI.

Recommended by 1 people across 1 different months, 2020 to 2022.

What it is

Paul Hudson of Hacking with Swift builds a drag and drop word game in SwiftUI from scratch in a roughly two-hour live session. It covers gestures, dragging views around, and detecting which views they are dropped onto using stored frames.

What people said

Recommends yuraist · 2020-04 · Stack Overflow · on “How to get frames (size and coordinates) for different views in SwiftUI?”

We can use the GeometryReader inside the overlay on needed views and store necessary frames in an array or as separate variables. ForEach(0..<n) { number in YourView() .overlay( GeometryReader { geometry in Color.clear .onAppear { self.buttonFrames[number] = geometry.frame(in: .global) } } ) } For more details, you can check this source: [LINK] and the full tutorial: [this video]

Cites hoshy · 2020-08 · Stack Overflow · on “Observe frame changes in SwiftUI”

I have view that can be dragged and dropped on top of other views (lets say categories). To detect which category view I'm on top of, I store their frames in a frames array, which happens in onAppear of their invisible overlays. (This is based on Paul Hudsons implementation in this [this video] tutorial). This works all nice and well, except when the position of those views change, e.g. in device orientation or windows resizing on iPad. This of course doesn't trigger onAppear, so the frames don't match anymore. HStack() { ForEach(categories) { category in ZStack { Circle() Rectangle() .foregroundColor(.clear) .overlay( GeometryReader { geo in Color.clear .onAppear { categoryFrames[index(for: cate

Cites sergio_veliz · 2022-04 · Stack Overflow · on “Ability to be informed when .onDrag() ends without any movement (i.e. immediately and before any drop occurs)”

on the Gesture have .onChange state and we can use it for understand have offset or not for example we can see location.x and location.y it's our new position and have current position for item (piece or text, image what you need) I read more articles before use it, if you have time you can read this: Hacking swift [LINK] Havinkg swift [LINK] [this video]