ios – 拖放 – UIGestureRecognizer – 查看添加的不是我想要的地方

我在splitViewController中有这2个tableViews(所有可用的服务和提供的服务).这个想法是,然后单元格移动到屏幕中心,它移动到服务提供表视图.

一切正常,除了触摸`.began,单元格被添加到覆盖navBar的tableView的顶部.

《ios – 拖放 – UIGestureRecognizer – 查看添加的不是我想要的地方》

On .changed跟着我的手指
《ios – 拖放 – UIGestureRecognizer – 查看添加的不是我想要的地方》

我希望在触摸开始的地方添加视图/单元格,在我正在触摸的“ServiceCell”之上.我尝试将它添加到splitViewController.view但我认为我的逻辑有问题.

这里的代码:

func didLongPressCell (gr: UILongPressGestureRecognizer) {
    let serviceCell:ServiceCell =  gr.view as! ServiceCell
    switch gr.state {
    case .began:

        let touchOffsetInCell = gr.location(in: gr.view)
        let dragEvent = DragganbleServiceCell()
        mDraggableServiceCell = dragEvent.makeWithServiceCell(serviceCell: serviceCell, offset: self.tableView.contentOffset, touchOffset: touchOffsetInCell)
        self.splitViewController?.view.addSubview(mDraggableServiceCell!)

    case .changed:
        let cp:CGPoint = gr.location(in: self.view)
        let newOrigin = CGPoint(x: (cp.x), y: (cp.y) - (mDraggableServiceCell?.touchOffset?.y)!)
        UIView.animate(withDuration: 0.1, animations: {
            self.mDraggableServiceCell?.frame = CGRect(origin: newOrigin, size: (self.mDraggableServiceCell?.frame.size)!)  
        })

    case .ended:
        let detailViewNC = self.splitViewController?.viewControllers[1] as! UINavigationController
        let detailView = detailViewNC.topViewController as! ServiceOfferedTableViewController
        if (mDraggableServiceCell?.frame.intersects(detailView.tableView.frame))! {

        onDragEnded(serviceCell:serviceCell)
        }else{

        mDraggableServiceCell?.removeFromSuperview()

        }
    default:
        print("Any other action?")
    }
}

在DraggableServiceCell中:

class DragganbleServiceCell: ServiceCell {

    var originalPosition:CGPoint?
    var touchOffset:CGPoint?

    func makeWithServiceCell(serviceCell:ServiceCell, offset:CGPoint, touchOffset:CGPoint)->DragganbleServiceCell{

        let newFrame =  CGRect(x: serviceCell.frame.origin.x, y: serviceCell.frame.origin.y, width: serviceCell.frame.size.width, height: serviceCell.frame.size.height)
        let dragCell = DragganbleServiceCell(frame: newFrame)
       dragCell.touchOffset = touchOffset
        dragCell.service = serviceCell.service
        dragCell.serviceName.text = serviceCell.service?.service_description
        return dragCell
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.containingView.backgroundColor = UIColor.rgb(230, green: 32, blue: 31)
        self.serviceName.textColor = .white
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

最佳答案 您的问题出在makeWithServiceCell(serviceCell:ServiceCell,offset:CGPoint,touchOffset:CGPoint) – > DragganbleServiceCell. serviceCell的框架相对于包含它的tableView而不是UISplitViewController的视图,您永远不会使用offset或touchOffset值来纠正它.因此,尝试以与.changed事件相同的方式调整框架.

您甚至可能会发现UIView上的convert(_:to :)方法可能对两个视图之间的CGPoint转换很有用.查看更多详情here.

点赞