Created
January 2, 2024 15:12
-
-
Save shmam/33a1ee26002443a87e8e270e818c63a4 to your computer and use it in GitHub Desktop.
Drag Gesture Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct TestCanvas: View { | |
@State private var currentGestureLocation: CGPoint = .zero | |
let colors = [ | |
Color.gray, | |
Color.gray, | |
Color.gray, | |
Color.gray | |
] | |
var body: some View { | |
ZStack { | |
VStack { | |
ForEach(colors.indices, id: \.self) { idx in | |
colors[idx] | |
.frame(maxWidth: 200, maxHeight: 50) | |
.overlay(GeometryReader { geo in | |
Color.clear | |
.onTapGesture { | |
} | |
}) | |
} | |
} | |
Circle() | |
.fill(.green) | |
.frame(width: 20, height: 20) | |
.position(currentGestureLocation) | |
} | |
.gesture( | |
DragGesture(minimumDistance: 0) | |
.onChanged { value in | |
currentGestureLocation = value.location | |
} | |
.onEnded { _ in | |
currentGestureLocation = .zero | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment