SwiftUI如何在具有手势的视图上启用按钮



我想在ZStack具有DragGesture的视图上放置按钮。但当我在视图上按下按钮时,它就不起作用了。


struct TmpView5: View {
@State var position: CGSize = CGSize(width: 200, height: 250)

var drag: some Gesture {
DragGesture()
.onChanged{ value in
self.position = CGSize(
width: value.startLocation.x
+ value.translation.width,
height: value.startLocation.y
+ value.translation.height
)
}
.onEnded{ value in
self.position = CGSize(
width: value.startLocation.x
+ value.translation.width,
height: value.startLocation.y
+ value.translation.height
)
}

}

var body: some View {

ZStack {

Rectangle()
.fill(Color.black)
.position(x: position.width, y: position.height)
.gesture(drag)

HStack {
Button(action: {
print("taped!!!")
}){
Image(systemName:"photo.on.rectangle")
}.padding(.trailing)


Button(action: {
print("taped!!!")
}){
Image(systemName:"a")
.padding(.trailing)
}

}
}
}
}

如果我删除手势(拖动(,我可以点击按钮。是否可以在矩形上添加手势并启用按钮?

工作正常,只是按钮彼此靠得太近,内部区域太小,所以为每个按钮添加更多的内部填充,如下所示(使用Xcode 12.1/iOS 14.1测试(

HStack {
Button(action: {
print("taped!!!")
}){
Image(systemName:"photo.on.rectangle").padding()    // << here !!
}

Button(action: {
print("taped!!!")
}){
Image(systemName:"a").padding()     // << here !!
}
}

最新更新