3

Is it possible to completely replace an element from draggable UITableViewCell to custom UIView?

For example, I drag a cell, and I want to display a circle when I click on the cell, and in the process of dragging, but until it is released, show on the element whether or not the element can be lowered to the end position.

I used UITableViewDragDelegate and UIDropInteraction, but could not find a cell replacement, only get the part from the cell using UIBezierPath to cut out part of the cell.

1 Answer 1

3

You can achieve that by implementing this delegate method

func tableView(_ tableView: UITableView,
   itemsForBeginning session: UIDragSession,
   at indexPath: IndexPath) -> [UIDragItem] {
       let provider = NSItemProvider()
           let item = UIDragItem(itemProvider: provider)
           
           // remove this code to drag whole cell
           ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
           item.previewProvider  = { () -> UIDragPreview? in
              let previewImageView = UIView()
              previewImageView.frame =  CGRect(x: 0,y: 0,width: 50,height: 50)
             previewImageView.layer.cornerRadius = previewImageView.bounds.maxX / 2
              return UIDragPreview(view: previewImageView)
           }
           /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          
              
              
           return [item]
   }

You will get this effect

enter image description here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.