I am trying to create a resizable shape by using DragGesture() and I can't find a way to limit the size of the shape when dragging.
Here is the current output. I don't want to cross the green line. https://i.stack.imgur.com/8XPZw.jpg
This is the current code:
struct ContentView: View {
var body: some View {
VStack {
CustomDraggableComponent()
.frame(width: 350, height: 350, alignment: .center)
.border(Color.green, width: 5)
}
}
struct CustomDraggableComponent: View {
@State var height: CGFloat = 200
@State var width: CGFloat = 200
var body: some View {
VStack {
Rectangle()
.fill(Color.red)
.frame(minWidth: width, maxWidth: width, minHeight: height, maxHeight: height)
HStack {
Spacer()
Rectangle()
.fill(Color.gray)
.frame(width: 80, height: 30)
.cornerRadius(10)
.overlay(Text("Drag"))
.gesture(
DragGesture()
.onChanged { value in
height = max(200, height + value.translation.height)
width = max(200, height + value.translation.width)
}
)
Spacer()
}
}
}
}
}