我用拖动手势创建了一个图像,但是当我将它转换为按钮时,全屏成为按钮,所以当我点击屏幕中的任何地方时,按钮将是动作
struct OwlFly: View {
private var bround = UIScreen.main.bounds
@State var isShow = false
@State private var location = CGPoint(x: 60, y: 60)
@GestureState private var startLocation: CGPoint? = nil
var simpleDrag: some Gesture {
DragGesture()
.onChanged { value in
var newLocation = startLocation ?? location
newLocation.x += value.translation.width
newLocation.y += value.translation.height
self.location = newLocation
self.location = value.location
}
.onEnded{ value in
if(value.translation.width > bround.size.width/2) {
self.location.x = bround.size.width - 30
}
else {
self.location.x = 30
}
}
.updating($startLocation) { (value, startLocation, transaction) in
startLocation = startLocation ?? location
}
}
var body: some View {
Button(action: {
self.isShow.toggle()
if(isFly) {
self.location.x = bround.width/2
}
else {
self.location.x = 30
}
}) { // I convert the Image to a label of button
Image("simpleDrag")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50)
.animation(.easeInOut)
.position(location)
.gesture(simpleDrag)
}
}
}
那是我的代码