Button 用按钮点击ZStack中的可点击图像
我有一个Button 用按钮点击ZStack中的可点击图像,button,swiftui,zstack,Button,Swiftui,Zstack,我有一个ZStack,上面有一个图像和一个关闭按钮。我不能用覆盖在上面的按钮使图像可点击 ZStack(alignment: .topTrailing) { NetworkImage(url: article.imageURL) .aspectRatio(contentMode: .fill) .frame(width: UIScreen.main.bounds.w
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(对齐:.topTrailing){
NetworkImage(url:article.imageURL)
.aspectRatio(内容模式:.fill)
.frame(宽度:UIScreen.main.bounds.width,
高度:UIScreen.main.bounds.height*0.5)
.scaleEffect(scrollViewContentOffset<0?
1+(-scrollViewContentOffset*0.005)
: 1,
锚点:。底部)
按钮(操作:{
self.presentationMode.wrappedValue.discouse()文件
}){
图像(系统名称:“xmark.circle.fill”)
.foregroundColor(.init(白色:0.9)).padding([.top,.training])
.font(.system(大小:topCloseButtonSize))
.offset(y:scrollViewContentOffset<0?
.超大+滚动视图内容偏移量:
(特大型)
}
}
我曾尝试向图像本身添加点击手势,但这会禁用关闭按钮上的可点击性。如何使ZStack保持得体,并使图像和关闭按钮都可点击?尝试向网络图像添加一个
ontapsignet
修改器(下面代码中的矩形)-它似乎可以按预期工作。我将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])
}
}
这也是我尝试过的解决方案。我无法再使用此解决方案点击按钮。我建议您在示例中添加更多代码-问题可能在于您正在使用的NetworkImage类或正在应用的偏移/比例修改器。我无法运行上面发布的代码,因为存在多个未知引用。如果您运行我上面发布的答案中的简化示例代码,它将实现您的目标。