Drag & Drop from TableView to Another View in Swift

╄→гoц情女王★ 提交于 2019-12-12 08:54:00

问题


I'm trying to copy an item from one UITableView to another View and I've been banging my head over this for the past 2 days and still i am not able to figure out how to accomplish this.

Here is a little sketch of my UI architecture

Here is what i am doing

  1. Long Press on a row in the tableview

  2. Create a snapshot of the image in the cell when long pressed

  3. Drag the snapshot to the View(Green area) outside the table view

  4. When released check whether the snapshot was dropped in the Green View Area.

I am able to do till the point of snapshot creation and when i try to drag the snapshot, i am not able to get the points in which the snapshot is dragged. When the drag is released, i need to check whether the last point of drag was inside the DROP AREA (Green Color).

Can anyone give some insights on how to solve the problem with some sample code...

Thanks in advance...


回答1:


Currently I does't have the time to test the code, but it should be enough to make sense.... You can do something like this:

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?")
        }
    }

}

Update on comment:

sure, one possibility would be to tag the fields... like this:

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
    //...
}



回答2:


  1. ditch ios 10 and earlier support raising deployment target to ios 11
  2. Adopt UITableViewDragDelegate protocol 2.1 [optionally] enable dragInteractionEnabled = true for table to have this working in iphone (by default drag works only on ipad)
  3. Study https://developer.apple.com/documentation/uikit/drag_and_drop/making_a_view_into_a_drop_destination to check how to make green area a drop destination
  4. Watch about 4 drag and drop related videos in wwdc 2017 or go through their ascii versions

As a bonus you can have drag and drop working between apps rather than just inside your app.



来源:https://stackoverflow.com/questions/38323948/drag-drop-from-tableview-to-another-view-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!