2

I'm using UIDragInteraction and it works perfectly.

let dragInteraction = UIDragInteraction(delegate: self)
myView.addInteraction(dragInteraction)
dragInteraction.isEnabled = true

As intended, the drag starts when I make a long press on myView. I now want to change this behavior. Instead of long press, I want the drag to start immediately on touching myView and moving my finger?

I've tried using a pan gesture to initiate the move but I don't know how to get the dragInteraction to start.

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panAction))
panGesture.minimumNumberOfTouches = 1
panGesture.maximumNumberOfTouches = 1
panGesture.cancelsTouchesInView = false
panGesture.delaysTouchesBegan = false
myView.addGestureRecognizer(panGesture)

Please help. Thanks.

Sujatha Girijala
  • 1,141
  • 8
  • 20
Felix Marianayagam
  • 2,634
  • 2
  • 9
  • 29

2 Answers2

1

Decrease the long-press duration right after adding the drag interaction:

myView.gestureRecognizers?.compactMap({ $0 as? UILongPressGestureRecognizer}).forEach { recognizer in
    recognizer.minimumPressDuration = .leastNonzeroMagnitude
}
Vladimir Grigorov
  • 10,903
  • 8
  • 60
  • 70
  • This works well !.... I put it in viewDidLoad() to modifiy the the trigger period for two views (to drag both ways), , where the view was a UITableView and another was a UICollectionView, and it made it very quick to trigger. .... (Note: The default longpress time is 0.5). I wasn't getting the result I wanted from this less concise approach: if let longPress = view?.gestureRecognizers?.first(where: { $0 is UILongPressGestureRecognizer }) as? UILongPressGestureRecognizer { longPress.minimumPressDuration = 0.1 } – – clearlight Aug 27 '23 at 18:56
0

This does not seem to be possible at the moment with the standard drag and drop implementation.

You could lower the minimumPressDuration of the UILongPressGestureRecognizer associated with the UIDragInteraction though. Further explanation for this can be found in this answer: set minimumPressDuration for UIDragInteraction

In addition to that you can always implement your own drag and drop interaction by using gesture recognizers, etc.. This might of course be tedious and not what you want.

Wizard
  • 295
  • 1
  • 4
  • 15