cho*_*rry 41 user-interaction swift swiftui
假设我有一个 SwiftUI 视图层次结构,如下所示:
ZStack() {
ScrollView {
...
}
Text("Hello.")
}
Run Code Online (Sandbox Code Playgroud)
该Text视图阻止触摸事件到达底层 ScrollView.
使用 UIKit,我会使用类似的东西.isUserInteractionEnabled来控制它,但我找不到任何方法来使用 SwiftUI 来做到这一点。
我试过在文本视图上添加一个Gesture带 aGestureMask的.none,但这似乎不起作用。
我希望我在这里遗漏了一些明显的东西,因为我需要在滚动视图的顶部放置一些状态信息。
Mih*_*aiL 45
使用 .allowsHitTesting() 怎么样?
https://developer.apple.com/documentation/swiftui/image/3269586-allowshittesting
根据我的理解,如果它被禁用,它应该将手势传递给后面的视图。
ZStack() {
ScrollView {
...
}
Text("Hello.").allowsHitTesting(false)
}
Run Code Online (Sandbox Code Playgroud)
LuL*_*aGa 27
View 上有一个修饰符来禁用或启用用户交互:
disabled(_ disabled: Bool)
zou*_*tre 17
将要“点击禁用”的视图放入 a 中Group,然后应用修改器.allowsHitTesting(false)以禁用与组内任何视图的交互。
Group {
Button("Hello")
}
.allowsHitTesting(false)
Run Code Online (Sandbox Code Playgroud)
要启用交互,请将修饰符切换为 true。
Thr*_*per 13
SwiftUI 2.0
ZStack {
// Your Stack to Disable User Interaction
}.disabled(showLoadingIndicator)
Run Code Online (Sandbox Code Playgroud)
你想确保你用某种颜色填充你的视图,除了清除颜色(你总是可以改变不透明度)。我还添加了模糊来隐藏元素。以下是对我有用的内容:
斯威夫特用户界面:
ZStack{
SomeView().blur(radius: 12)
Rectangle()
.fill(Color.white.opacity(0))
.allowsHitTesting(false)
}
Run Code Online (Sandbox Code Playgroud)
另一种禁用用户交互的方法,如滚动或按钮点击,但将操作附加到用户点击(例如,向用户发送一条消息,表明此功能即将进入或在付费专区后面):
斯威夫特用户界面:
VStack{
SomeView().blur(radius: 12)
}
.contentShape(Rectangle())
.onTapGesture {
print("No access!")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16289 次 |
| 最近记录: |