Mikhail S
Mikhail S

Reputation: 4103

iOS Drag & Drop. How to replace a cell with a custom view while dragging

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.

Upvotes: 3

Views: 1200

Answers (1)

Jawad Ali
Jawad Ali

Reputation: 14407

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

Upvotes: 3

Related Questions