有条件地激活 SwiftUI 中的手势
我正在寻找一种方法来启用/禁用 DragGesture 或根据 @State private var isDraggable:Bool 有条件地将手势传递到 swiftUI 中的 MyView()
因为 MyView() 有一些在 Appear() 和 Disappear 上重置的参数(),我不能只做
If isDraggable { MyView() }else{MyView().gesture() }
How DragGesture is Implementing in my code
MyView().gesture(DragGesture(minimumDistance: 0) .onChanged { value in} )
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} )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
GestureMask
并在可以使用的手势之间进行选择 .all、.none、.subviews 或 .gesture
.gesture
在您的情况下将是DragGesture< /代码>。
.subviews
将允许MyView
内的任何手势.all
既是DragGesture
又是MyView 内的任何手势
(这是默认设置)根据您的用例更改
activeGestures
变量You can use
GestureMask
and choose between gesturesyou can have .all, .none, .subviews, or .gesture
.gesture
in your case would be theDragGesture
..subviews
would allow any gestures insideMyView
.all
is both theDragGesture
AND any gestures insideMyView
(this is the default)Change the
activeGestures
variable per your use case我认为这就是您正在寻找的:它对我来说非常有效。您只需添加
isEnabled:
作为方法手势的第二个参数(DragGesture()
是第一个参数),它是一个布尔值(您的isDraggable
),非常简单: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 (yourisDraggable
), very straightforwardly: