ios – How to detect when finger “exits” element bounds during drag gesture?


I am working on a custom button component for which I need to use drag gesture to set various states of custom animation, below is simplified code of where I am at the moment. Problem here is that action associated to the button gets executed during .onEnded phase. This means that user can drag their finger outside of the button and upon releasing it action will be called. I’d like to detect if finger is in bounds of the button somehow and if user drags it out, not execute action and toggle taping state to off, essentially canceling the tap when finger is not on the button.

I tried wrapping GeometryReader around my HStack, but it reported weird values (much higher values than what the real size of the button is) and it made button component expand to full screen width and height.

    HStack {
      Text("Sample Text")
    }
    .scaleEffect(taping ? 0.9 : 1)
    .gesture(
      DragGesture(minimumDistance: 0)
        .onChanged { pressed in
          taping.toggle()
        }
        .onEnded { pressed in
          taping.toggle()
          // Execute tap action here
        }
    )

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img