swift - 滚动视图下的 SwiftUI 点击
问题描述
绿色的点击手势不起作用。有人对此有解决方案吗?
ZStack {
Color.green
.onTapGesture {
print("green")
}
ScrollView {
VStack {
Spacer(minLength: 500)
Color.red
.onTapGesture {
print("red")
}
.frame(height: 800)
}
}
}
解决方案
你想要的东西是不可能的,因为 ScrollView 在绿色视图之上,但是有这样的方法:
struct ContentView: View {
func greenFunction() { print("green") }
var body: some View {
ZStack {
Color.green.onTapGesture { greenFunction() }
ScrollView {
VStack(spacing: 0) {
Color.white.opacity(0.01).frame(height: 500).onTapGesture { greenFunction() }
Color.red.frame(height: 800).onTapGesture { print("red") }
}
}
}
}
}
推荐阅读
- php - 何时调用 restore_error_handler()?
- python - Pandas:json多个文件并以奇怪的格式连接
- icalendar - 在 .ics 文件中仅显示一个 VEVENT(第一个)
- c# - ReactiveUI 测试中的调度程序
- webforms - 如何使用 CSP(内容安全策略)将 WebForms 项目中动态创建的脚本列入白名单?
- node.js - 如何将文件发送到在节点 js 中使用 multer 的 api?
- c# - c# webapi查询字符串路由
- mysql - 使用 Node.js 搜索 PDF 文本并返回一个片段
- iis - 如果 stdoutLogEnabled 为真,Serilog 控制台是否会写入 IIS 日志文件?
- node.js - 删除数据库中的记录