I have a draggable text and I have a red rectangle . I am trying to have that draggable text within that red rectangle and to only allow it to be dragged inside that rectangle . I have this code so far and it does everything that I need except the restricting the draggable text part to be inside the red rectangle, any suggestions on how I can confine the draggable text to only be inside the red rectangle ?
struct DraginContainerView: View {
@State var viewState = CGSize.zero
var body: some View {
VStack {
Rectangle()
.fill(.red)
.frame(width: 200, height: 200)
Text("My Text")
.foregroundColor(.black)
.offset(x: viewState.width, y: viewState.height)
.gesture(
DragGesture()
.onChanged { value in
viewState = value.translation
}.onEnded { value in
self.viewState = value.translation
}
)
}
}
}