简体   繁体   中英

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?

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.


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

    /* ... */

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM