5

I have a UIImageView which I have made it draggable by adding it an UIDragInteraction as shown in the code below. Now, I can drag it in any direction, however, I want the draggable item to be restricted to the y-axix(verticle). i.e. no motion along the x-axis. How can I achieve that?

    let dragInteraction = UIDragInteraction(delegate: self)
    dragInteraction.isEnabled = true
    dragView.addInteraction(dragInteraction)
rahulg
  • 2,183
  • 3
  • 33
  • 47
  • are you looking to use the UIDragInteraction to move the dragView around on the screen? or are you trying to implement Drag and Drop? – Alan S Sep 04 '18 at 11:43
  • @AlanSarraf I want to implement Drag and drop. Able to achieve everything, however, cannot restrict the movement of the draggable `UIView` to one axis – rahulg Sep 05 '18 at 13:00
  • Looking over the functions they have in the implementation of drag and drop, it doesn't seem like there is anything that gives you the frame or position of the item you are moving. The UIInteraction class only provides which views the item is moving from and to. I don't think you will be able to limit the movement to one axis with their functions. I'm not sure if it would be possible or simple, but you might be able to override touchesBegan, touchesMoved and touchesEnded and adjust the touches on the screen while dragging to touches that all have the same x point, but varying y points. – Alan S Sep 05 '18 at 13:20
  • @AlanSarraf well I can access the location of the items using `UIDragSession`'s `location(in: view)` function, as well can access the previews with the `items` array. However, am not able to change the previews position – rahulg Sep 05 '18 at 15:43
  • Did you ever find a solution? – zaitsman Aug 27 '23 at 00:29

0 Answers0