button - 带有按钮的 ZStack 中的可点击图像
问题描述
我有ZStack
一个图像和一个覆盖它的关闭按钮。我无法使用覆盖它的按钮使图像可点击。
ZStack(alignment: .topTrailing) {
NetworkImage(url: article.imageURL)
.aspectRatio(contentMode: .fill)
.frame(width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height * 0.5)
.scaleEffect(scrollViewContentOffset < 0 ?
1 + (-scrollViewContentOffset * 0.005)
: 1,
anchor: .bottom)
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}){
Image(systemName: "xmark.circle.fill")
.foregroundColor(.init(white: 0.9)).padding([.top, .trailing])
.font(.system(size: topCloseButtonSize))
.offset(y: scrollViewContentOffset < 0 ?
.extraLarge + scrollViewContentOffset :
.extraLarge)
}
}
我试图向图像本身添加一个点击手势,但这会禁用关闭按钮上的可点击性。如何保持 ZStack 完好无损,并让图像和关闭按钮都可以点击?
解决方案
尝试向onTapGesture
NetworkImage 添加一个修饰符(下面代码中的矩形) - 它似乎按预期工作。我简化了 NetworkImage 以使用标准矩形,但点击结果应该是相同的。在 Xcode 12.1 上测试。
ZStack(alignment: .topTrailing) {
Rectangle() // Use NetworkImage here - simplified to Rectangle for testing
.aspectRatio(contentMode: .fill)
.frame(width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height * 0.5)
.onTapGesture(count: 1, perform: {
print("RECTANGLE TAPPED!")
})
Button(action: {
print("CLOSE TAPPED")
}){
Image(systemName: "xmark.circle.fill")
.foregroundColor(.init(white: 0.9)).padding([.top, .trailing])
}
}
推荐阅读
- python - 如何将数组打印为特定格式?
- mysql - 尝试配置 ODBC 驱动程序时权限被拒绝
- r - 使用填充参数覆盖直方图
- android - 某些设备上导致强制关闭的 Android 充气错误
- php - php strpos函数的多个输入?
- html - 类型为“提交”的链接的边框在 Chrome 中没有消失?
- python - 如何在一个事务中进行三个 POST 调用?
- javascript - blueimp Gallery:没有或空列表作为第一个参数提供
- verilog - Sequential instatiation of Verilog Modules
- sql - 带有“Like”和“%___%”的 SAS SQL