I've been trying out the new iOS 11 drag and drop feature. It's great, but it works only on iPad. Apple claims that it works also on the iPhone, but I can't get it working there? Is Apple's claim false, or am I doing something wrong?
Using drag and drop, people can move or duplicate selected photos, text, and other content by dragging the selection from one location to another. iOS, iPadOS, and macOS support drag and drop through gestures on the touchscreen, interactions with a pointing device, and through full keyboard-access mode.
The drag and drop feature in iOS was released in 2017 in iOS 11 and worked only on iPad, but now with the last-year iOS 15 update, the drag and drop feature is available to use on iPhones as well.
Press and hold on the first image or link you want to drag, then pull your finger down a little bit. Once you tap and hold on the media you want to move, the usual pop-up menu for sharing options will appear. Ignore that, and jerk your finger down the screen a little without letting go.
You're installing a UIDragInteraction object on some view, right? Well, by default, that drag interaction's isEnabled property is false on an iPhone (in accordance with the device-dependent value of the isEnabledByDefault class property).
So to switch on drag and drop on the iPhone, just set the drag interaction's isEnabled to true when you create it:
override func viewDidLoad() {
super.viewDidLoad()
let dragger = UIDragInteraction(delegate: self)
self.dragView.addInteraction(dragger)
dragger.isEnabled = true // for iPhone: presto, we've got drag and drop!
}
Similarly for a table view or collection view, as pointed out by the other answer, you would need to set its dragInteractionEnabled to true, as it too is false by default on an iPhone.
With Swift 4 and iOS 11, according to your needs, you can pick one of the following ways in order to solve your problem.
UITableView on iPhoneUITableView has a property called dragInteractionEnabled. dragInteractionEnabled has the following declaration:
var dragInteractionEnabled: Bool { get set }
A Boolean value indicating whether the table view supports drags and drops between apps.
The default value of this property is
trueon iPad andfalseon iPhone. Changing the value totrueon iPhone makes it possible to drag content from the table view to another app on iPhone and to receive content from other apps.
The following code shows how to use dragInteractionEnabled in order to allow drag and drop interactions for UItableView on iPhone:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
tableView.dragInteractionEnabled = true
}
}
UICollectionView on iPhoneUICollectionView has a property called dragInteractionEnabled. dragInteractionEnabled has the following declaration:
var dragInteractionEnabled: Bool { get set }
A Boolean value indicating whether the collection view supports drags and drops between apps.
The default value of this property is
trueon iPad andfalseon iPhone. Changing the value totrueon iPhone makes it possible to drag content from the collection view to another app on iPhone and to receive content from other apps.
The following code shows how to use dragInteractionEnabled in order to allow drag and drop interactions for UICollectionView on iPhone:
class CollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
/* ... */
collectionView?.dragInteractionEnabled = true
}
}
UIImageView on iPhoneUIDragInteraction has a property called isEnabled. isEnabled has the following declaration:
var isEnabled: Bool { get set }
A Boolean value that specifies whether the drag interaction responds to touches and is allowed to participate in a drag activity.
The following code shows how to use isEnabled in order to allow drag interaction in addition to drop interaction for UIImageView on iPhone:
class ViewController: UIViewController, UIDragInteractionDelegate, UIDropInteractionDelegate {
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.image = UIImage(named: "MyImage")
imageView.isUserInteractionEnabled = true
imageView.contentMode = .scaleAspectFit
imageView.frame = view.bounds
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let dropInteraction = UIDropInteraction(delegate: self)
imageView.addInteraction(dropInteraction)
let dragInteraction = UIDragInteraction(delegate: self)
dragInteraction.isEnabled = true
imageView.addInteraction(dragInteraction)
}
/* ... */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With