Drag and Drop - UIGestureRecognizer - view added not where I want

I have these 2 tableViews (all available services and services offered) in splitViewController. The idea is that then the cell moves to the center of the screen, it moves to the table view offered by the service. Everything works, except by touching the `.began, cell is added to the top of the View table covering the navBar. enter image description here

In .changedhe follows the finger enter image description here

I would like for view / cell to be added when the touch starts, on top of "ServiceCell" I touch. I tried adding it to splitViewController.view, but I think something is wrong with my logic.

Here is the code:

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

In 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")
    }
}
+4
2

makeWithServiceCell(serviceCell:ServiceCell, offset:CGPoint, touchOffset:CGPoint) -> DragganbleServiceCell. serviceCell tableView, UISplitViewController, offset touchOffset, . , .changed.

, , convert(_:to:) UIView CGPoint . .

+2

self.splitViewController?.view self.view, .

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 .changed:
    let cp:CGPoint = gr.location(in: self.splitViewController?.view)
    UIView.animate(withDuration: 0.1, animations: {
        self.mDraggableServiceCell?.center = cp  
    })

.

0

Source: https://habr.com/ru/post/1663997/


All Articles