Reputation: 153
I'm looking for a way to able/disable DragGesture or to pass conditionally pass the Gesture to MyView() in swiftUI according to @State private var isDraggable:Bool
Since MyView() has some parameters that are reset on Appear() and Disappear(), I can not just do
If isDraggable { MyView() }else{MyView().gesture() }
How DragGesture is implemented in my code
MyView().gesture(DragGesture(minimumDistance: 0) .onChanged { value in} )
Upvotes: 2
Views: 2007
Reputation: 51
I think this is what you are looking for: it worked very well for me. You just add isEnabled:
as second param of the method gesture (DragGesture()
being the first param), which is a boolean (your isDraggable
), very straightforwardly:
MyView().gesture(
DragGesture(minimumDistance: 0)
.onChanged { value in},
isEnabled: isDraggable)
Upvotes: 1
Reputation: 29242
You can use GestureMask
and choose between gestures
you can have .all, .none, .subviews, or .gesture
.gesture
in your case would be the DragGesture
.
.subviews
would allow any gestures inside MyView
.all
is both the DragGesture
AND any gestures inside MyView
(this is the default)
import SwiftUI
struct ConditionalGestureView: View {
@State var activeGestures: GestureMask = .subviews
var body: some View {
MyView()
.gesture(DragGesture(), including: activeGestures)
}
}
Change the activeGestures
variable per your use case
Upvotes: 7