拖曳从Swift中的TableView到另一个视图 [英] Drag & Drop from TableView to Another View in Swift

查看:142
本文介绍了拖曳从Swift中的TableView到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个项目从一个UITableView复制到另一个视图,而且在过去的两天中我一直在头顶,而我仍然无法弄明白如何实现这一点。

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.

这是我的UI架构的一个小图。

Here is a little sketch of my UI architecture

这是我在做什么


  1. 长按一下

  1. Long Press on a row in the tableview

长时间在单元格中创建图像的快照

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

拖动在表视图之外的视图(绿色区域)的快照

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

发布时检查快照是否在绿色视图区域中丢弃。

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

我可以做到快照创建的点,当我尝试拖动快照时,我无法得到积分其中快照被拖动。当拖动被释放时,我需要检查最后一个拖动点是否位于DROP 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...

提前感谢...

推荐答案

目前我没有时间来测试代码,但这应该足够有意义....你可以这样做:

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

}

更新评论:

肯定有一种可能性标记字段,如下所示:

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

这篇关于拖曳从Swift中的TableView到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆