How to make iOS 11 drag and drop on iPhone

I tried the new drag and drop function of iOS 11. This is great, but it only works on the iPad. Apple claims that it works on the iPhone, but I can’t get it to work there? Is Apple false or am I doing something wrong?

+4
source share
2 answers

You set the UIDragInteraction object to some kind, right? By default, the drag interaction property isEnabledhas a value falseon the iPhone (according to the device-dependent property value isEnabledByDefault).

So, to enable drag and drop on the iPhone, just set the interaction with the body isEnabledto true when creating 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!
}

, , dragInteractionEnabled true, false iPhone.

+4

Swift 4 iOS 11, , , .


# 1. UITableView iPhone

UITableView , dragInteractionEnabled. dragInteractionEnabled :

var dragInteractionEnabled: Bool { get set }

, , .

true iPad false iPhone. true iPhone iPhone .

, dragInteractionEnabled, UITableView iPhone:

class TableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        /* ... */

        tableView.dragInteractionEnabled = true
    }

}

# 2. UICollectionView iPhone

UICollectionView , dragInteractionEnabled. dragInteractionEnabled :

var dragInteractionEnabled: Bool { get set }

, , .

true iPad false iPhone. true iPhone iPhone .

, dragInteractionEnabled, UICollectionView iPhone:

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        /* ... */

        collectionView?.dragInteractionEnabled = true
    }

}

# 3. UIImageView iPhone

UIDragInteraction , isEnabled. isEnabled :

var isEnabled: Bool { get set }

, , .

, isEnabled, UIImageView 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)
    }

    /* ... */

}
+4

Source: https://habr.com/ru/post/1686300/


All Articles