本文介绍了如何在 SwiftUI 视图上禁用用户交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个如下所示的 SwiftUI 视图层次结构:

Let's say I have a SwiftUI view hierarchy that looks like this:

ZStack() {
    ScrollView {
        ...
    }
    Text("Hello.")
}

Text 视图阻止触摸事件到达底层 ScrollView.

The Text view blocks touch events from reaching the underlying ScrollView.

使用 UIKit,我会使用类似 .isUserInteractionEnabled 的东西来控制它,但我找不到任何使用 SwiftUI 的方法.

With UIKit, I'd use something like .isUserInteractionEnabled to control this, but I can't find any way to do this with SwiftUI.

我尝试在文本视图上添加一个 Gesture.noneGestureMask,但这似乎不起作用.

I've tried adding a Gesture with a GestureMask of .none on the text view, but that doesn't seem to work.

我希望我在这里遗漏了一些明显的东西,因为我需要在滚动视图的顶部放置一些状态信息.

I hope I'm missing something obvious here, because I need to put some status information on top of the scroll view.

推荐答案

使用 .allowsHitTesting() 怎么样?

What about using the .allowsHitTesting()?

https://developer.apple.com/documentation/swiftui/image/3269586-allowshittesting

根据我的理解,如果它被禁用,它应该将手势传递给后面的视图.

From my understanding it should pass the gesture to the view behind if it's disabled.

ZStack() {
    ScrollView {
        ...
    }
    Text("Hello.").allowsHitTesting(false)
}

这篇关于如何在 SwiftUI 视图上禁用用户交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 08:46