Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view for Drag and Drop

I have set up my UITableView to use the new Drag and Drop APIs.

if #available(iOS 11, *) {
    self.tableView.dragDelegate = self
    self.tableView.dropDelegate = self
    self.tableView.dragInteractionEnabled = true
    navigationController?.navigationBar.prefersLargeTitles = false
}

Now, I implemented the method below to be able to use custom views for the d&d.

@available(iOS 11.0, *)
func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {
    print("Custom Preview method called!")
    let test = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))
    test.text = "sfgshshsfhshshfshsfh"
    let dragView = interaction.view!
    let dragPoint = session.location(in: dragView)
    let target = UIDragPreviewTarget(container: dragView, center: dragPoint)
    return UITargetedDragPreview(view: test, parameters:UIDragPreviewParameters(), target:target)
}

However, this method never gets called. I never see the print() or my custom view. Any ideas as to what I'm doing wrong?

like image 451
user4992124 Avatar asked Jul 17 '17 09:07

user4992124


People also ask

How do I drag and drop a view on Android?

A drag and drop operation starts when the user makes a UI gesture that your app recognizes as a signal to start dragging data. In response, the app notifies the system that a drag and drop operation is starting. The system calls back to your app to get a representation of the data being dragged (a drag shadow).

What is drag and drop in GUI?

In computer graphical user interfaces, drag and drop is a pointing device gesture in which the user selects a virtual object by "grabbing" it and dragging it to a different location or onto another virtual object.

What is drag and drop window?

Drag and drop is an intuitive way to transfer data within an application or between applications on the Windows desktop. Drag and drop lets the user transfer data between applications or within an application using a standard gesture (press-hold-and-pan with the finger or press-and-pan with a mouse or a stylus).

Is SwiftUI drag and drop?

SwiftUI provides first-class support for drag and drop in the List collection, as well as the API for any view to support drag and drop.


1 Answers

You have to set previewProvider property when creating the UIDragItem.

let dragItem = UIDragItem(...)
dragItem.previewProvider = {
    print("Custom Preview method called!")
    let test = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))
    test.text = "sfgshshsfhshshfshsfh"
    return UIDragPreview(view: test)
}

See: https://developer.apple.com/documentation/uikit/uidragitem/2890972-previewprovider?language=objc

like image 85
Rodrigo Borges Avatar answered Sep 29 '22 13:09

Rodrigo Borges