我在iOS上有十年的经验,但对Swift/SwiftUI来说还是新手,所以请温柔一点.有一件事让我感到困惑,那就是为什么如果不透明度设置为零或设置为0,视图(我try 了一些,但让我们使用TextView()为例)似乎不会被绘制.背景( colored颜色 .清晰).
我试图实现的是在HStack中放置两个按钮,作为ZStack中的第一个成员(所以"后退"最多),以确定用户何时双击屏幕的左侧或右侧,但我不希望实际显示任何内容,只需检测点击即可.我的黑客解决方案是将不透明度设置为0.001.在不透明度设置下,什么都看不见,我可以用它来检测.OnTap手势(计数:2),但这肯定不是最佳方式.在UIKit land中,我很想用屏幕尺寸来处理点击坐标,以确定它落在哪一侧,但我不认为这是非常"快速"的,即使不包装UIKit类也是可能的.努力学习,尽可能做到"纯快捷".
public var body: some View {
ZStack {
HStack {
VStack {
Text("").frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
.opacity(0.0001) //hack workaround, setting opacity to 0.0 and it's like the view isn't even drawn or present (the onTapGesture doesn't get recognized because the VStack has no size when the Text frame is zero and that's the only point! Interestingly setting the background to Color.clear creates the same issue
}.onTapGesture(count: 2) {
print("got a double tap on the LEFT side scroll button")
}
VStack {
Text("").frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.yellow)
.opacity(0.0001)//hack workaround, setting opacity to 0.0 and it's like the view isn't even drawn or present (the onTapGesture doesn't get recognized because the VStack has no size when the Text frame is zero and that's the only point! Interestingly setting the background to Color.clear creates the same issue
}.onTapGesture(count: 2) {
print("got a double tap on the RIGHT side scroll button")
}
}
[subsequent members of the ZStack removed as they're not relevant to the question]
}
}
我想我的问题是:
a) 如何使用隐形但仍被布置的手势来检测?
b) 如果不透明度为零或背景设置为"清除",为什么Text()和其他视图基本上不存在?
谢谢