Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get iOS 11 drag and drop working on the iPhone

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?

like image 585
matt Avatar asked Sep 24 '17 16:09

matt


People also ask

Does drag and drop work on iPhone?

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.

Does drag and drop work on iPhone 8?

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.

How do you drag and drop on iOS 15?

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.


2 Answers

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.

like image 162
matt Avatar answered Nov 02 '22 17:11

matt


With Swift 4 and iOS 11, according to your needs, you can pick one of the following ways in order to solve your problem.


#1. Allow drag and Drop interactions for UITableView on iPhone

UITableView 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 true on iPad and false on iPhone. Changing the value to true on 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
    }

}

#2. Allow drag and drop interactions for UICollectionView on iPhone

UICollectionView 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 true on iPad and false on iPhone. Changing the value to true on 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
    }

}

#3. Allow drag and drop interactions for UIImageView on iPhone

UIDragInteraction 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)
    }

    /* ... */

}
like image 44
Imanou Petit Avatar answered Nov 02 '22 17:11

Imanou Petit