How to enable UIDragInteraction on Custom UIViews

2019-07-15 20:54发布

问题:

I've been trying to use Apple's new API to enable Drag interactions on Custom UIViews. I've managed to make it work when testing using iPad, but when I run the same app in my iPhone 7 - ios 11 beta 3, I'm not able to drag my custom UIViews.

I've noticed that we need to enable drag interaction when using in UICollectionView and UITableViews

collectionView.dragInteractionEnabled = true tableView.dragInteractionEnabled = true

but I can't manage to find anything in the documentation or anywhere about enabling it in custom UIViews to make it work on th iPhone. Does anyone know a way to enable drag interaction for custom UIViews for the iPhone?

Thanks

回答1:

let dragInteraction: UIDragInteraction = ...
dragInteraction.isEnabled = true

view.addInteraction(dragInteraction)

Check this out

UIDragInteraction.isEnabledByDefault https://developer.apple.com/documentation/uikit/uidraginteraction/2891051-isenabledbydefault