简体   繁体   English

SwiftUI 出现 onChanged 时 DragGesture 不会终止

[英]SwiftUI DragGesture does not terminate when .onChanged is present

I'm attempting to drag a view (the ZStack below) using the dragPiece gesture in the following code.我试图在以下代码中使用dragPiece手势拖动视图(下面的ZStack )。 If I have the .onChanged modifier commented out, this code works fine, in the sense that the view ends up repositioned.如果我注释掉了.onChanged修饰符,那么这段代码可以正常工作,因为视图最终会重新定位。 But when .onChanged is uncommented and active, the drag gesture seems to get stuck, repeatedly printing out the same width and height values, and the .onEnded modifier is never executed.但是当.onChanged取消注释并处于活动状态时,拖动手势似乎卡住了,重复打印出相同的widthheight值,并且永远不会执行.onEnded修饰符。 This seems like it should be straightforward, so clearly I'm missing something.这看起来应该很简单,所以很明显我遗漏了一些东西。 Any help will be appreciated.任何帮助将不胜感激。

struct PieceView: View {
    var id: String
    @State private var orientation: Int
    @State private var dragOffset = CGSize.zero
    @State var selected = false
    var dragPiece: some Gesture {
        DragGesture()
            .onChanged { value in
                dragOffset = value.translation
                print("width: \(dragOffset.width), height: \(dragOffset.height)")
            }
            .onEnded{ value in
                print("\(id) dragged")
                dragOffset = value.translation
                print("width: \(dragOffset.width), height: \(dragOffset.height)")
            }
    }
    var body: some View {
        ZStack {
            Image(id + "\(orientation)")
            Image("boardSquare")
                .padding(0)
                .gesture(dragPiece)
         }
        .offset(dragOffset)
    }
    init() {
        id = ""
        orientation = 0
    }
    init(id: String, orientation: Int = 0, gesture: String = "") {
        self.id = id
        self.orientation = orientation
    }
}

It appears that .offset(dragOffset) must appear before .gesture(dragPiece) .看来.offset(dragOffset)必须出现在.gesture(dragPiece)之前。 The following code works as expected:以下代码按预期工作:

struct PieceView: View {
    var id: String
    @State private var orientation: Int
    @State private var dragOffset = CGSize.zero
    @State var selected = false
    var dragPiece: some Gesture {
        DragGesture()
            .onChanged { value in
                dragOffset = value.translation
                print("width: \(dragOffset.width), height: \(dragOffset.height)")
            }
            .onEnded{ value in
                print("\(id) dragged")
                dragOffset = value.translation
                print("width: \(dragOffset.width), height: \(dragOffset.height)")
            }
    }
    var body: some View {
        ZStack {
            Image(id + "\(orientation)")
            Image("boardSquare")
                .padding(0)
                .offset(dragOffset)
                .gesture(dragPiece)
         }
    }
    init() {
        id = ""
        orientation = 0
    }
    init(id: String, orientation: Int = 0, gesture: String = "") {
        self.id = id
        self.orientation = orientation
    }
}

In my case, this leaves me with some other UI design issues, because I want the gesture to apply to Image("boardSquare") but the whole ZStack to be dragged.就我而言,这给我留下了一些其他 UI 设计问题,因为我希望将手势应用于Image("boardSquare")但要拖动整个ZStack But that's a separate issue, and at least now I know what the problem was with my current code.但这是一个单独的问题,至少现在我知道我当前代码的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM