有条件地激活 SwiftUI 中的手势

发布于 2025-01-13 21:41:28 字数 392 浏览 3 评论 0原文

我正在寻找一种方法来启用/禁用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2025-01-20 21:41:28

您可以使用 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 变量

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

尘曦 2025-01-20 21:41:28

我认为这就是您正在寻找的:它对我来说非常有效。您只需添加 isEnabled: 作为方法手势的第二个参数(DragGesture() 是第一个参数),它是一个布尔值(您的 isDraggable ),非常简单:

MyView().gesture(
        DragGesture(minimumDistance: 0)
            .onChanged { value in},
        isEnabled: 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 (your isDraggable), very straightforwardly:

MyView().gesture(
        DragGesture(minimumDistance: 0)
            .onChanged { value in},
        isEnabled: isDraggable)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文