Dragging from TableView to another view in Swift

I am trying to copy an element from one UITableView to another view, and for the last two days I have been racking my brains over this, but still cannot figure out how to achieve this.

Here is a small sketch of my interface architecture

enter image description here

That's what I'm doing

  1. Long press on a row in a table view

  2. Create a snapshot of the image in the cell by long pressing

  3. Drag the picture into the view (green area) outside the table view.

  4. After release, check if the picture was taken in the green viewing area.

, , , . , , ( ).

- , ...

...

+5
2

, , ... - :

class ViewController: UIViewController, UITableViewDataSource {

    private var dragView: UIView?
    @IBOutlet weak var dropZone: UIView!

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
        cell.contentView.addGestureRecognizer(lpGestureRecognizer)

        return cell
    }

    func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
        switch recognizer.state {
        case .Began:
            if let cellView: UIView = recognizer.view {
                cellView.frame.origin = CGPointZero
                dragView = cellView
                view.addSubview(dragView!)
            }
        case .Changed:
            dragView?.center = recognizer.locationInView(view)
        case .Ended:
            if (dragView == nil) {return}

            if (CGRectIntersectsRect(dragView!.frame, dropZone.frame)) {
                if let cellView: UIView = (dragView?.subviews[0])! as UIView {
                    cellView.frame.origin = CGPointZero
                    dropZone.addSubview(cellView)
                }

                dragView?.removeFromSuperview()
                dragView = nil

                //Delete row from UITableView if needed...
            } else {
                //DragView was not dropped in dropszone... Rewind animation...
            }
        default:
            print("Any other action?")
        }
    }

}

:

, - ... :

private let imageViewTag: Int = 997
private let textLabelTag: Int = 998
private let detailTtextLabelTag: Int = 999

//...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //...
    cell.imageView?.tag = imageViewTag
    cell.textLabel?.tag = textLabelTag
    cell.detailTextLabel?.tag = detailTtextLabelTag
    //...

}

func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
    //...
    case .Ended:
    let cellImageView: UIImageView? = recognizer.view?.viewWithTag(imageViewTag) as? UIImageView
    let cellTextLabel: UITextField? = recognizer.view?.viewWithTag(textLabelTag) as? UITextField
    let cellDetailTextLabel: UITextField? = recognizer.view?.viewWithTag(detailTtextLabelTag) as? UITextField
    //...
}
+7
  1. ditch ios 10 , ios 11
  2. UITableViewDragDelegate 2.1 [] dragInteractionEnabled = true , iphone ( ipad)
  3. https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination, ,
  4. 4 , , wwdc 2017 ascii

, .

+1

All Articles