Home > OS >  How to know if draged down or up while using DragGesture() in SwiftUI?
How to know if draged down or up while using DragGesture() in SwiftUI?

Time:04-26

I am using DragGesture() in my SwiftUI code..which working fine..but for one logic i want to know if its drag down or Up, is there any way to detect this?

    let drag = DragGesture()
        .onChanged { value in

            //some logic

        }
        .onEnded { value in
            // some logic 
            expand.toggle()
        }

CodePudding user response:

A possible approach is to have additional state property to store value.location.y coordinate and update dependent state comparing previously stored and currently received in drag value:

Tested with Xcode 13.3 / iOS 15.4

demo

Here is main part:

.gesture(DragGesture()
    .onChanged { value in
        let newValue = value.location.y   // fetch prev !!
        if let prev = self.current {
            self.isUp = prev > newValue   // << here !!
        }
        self.current = newValue   // store curr !!
        self.offsetY = value.translation.height
    }

Complete findings and code is here

  • Related