我正在寻找一种方法来启用/禁用 DragGesture 或根据 @State private var isDraggable:Bool 有条件地将手势传递到 swiftUI 中的 MyView()
由于 MyView() 有一些在 Appear() 和 Disappear() 上重置的参数,我不能这样做
If isDraggable { MyView() }else{MyView().gesture() }
DragGesture 在我的代码中是如何实现的
MyView().gesture(DragGesture(minimumDistance: 0) .onChanged { value in} )
您可以使用
GestureMask
并在手势之间进行选择
您可以拥有 .all、.none、.subviews 或 .gesture
在你的情况下,.gesture
就是DragGesture
。
.subviews
允许在 MyView
内进行任何手势
.all
既是 DragGesture
又是 MyView
内的任何手势(这是默认设置)
import SwiftUI
struct ConditionalGestureView: View {
@State var activeGestures: GestureMask = .subviews
var body: some View {
MyView()
.gesture(DragGesture(), including: activeGestures)
}
}
根据您的用例更改
activeGestures
变量
MyView().gesture(
DragGesture(minimumDistance: 0)
.onChanged { value in},
isEnabled: isDraggable)