Benny Wijaya
Benny Wijaya

Reputation: 217

Hide UICollectionViewCell Drag Preview

i'm implementing drag and drop in collection view by using UICollectionViewDragDelegate and tried to hiding the drag preview while dragging

i manage to hide it by using this line of codes after following this thread Custom View for UICollectionViewCell Drag Preview:

public func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let dragItem = UIDragItem(itemProvider: NSItemProvider())
        dragItem.previewProvider = {
            return nil
        }
}

but the drag preview is still displayed when it's being lifted and the only method that allow me to modify the drag preview during lifting is

public func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {

        let previewParameters = UIDragPreviewParameters()
        previewParameters.visiblePath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 50), cornerRadius: 0)
        previewParameters.backgroundColor = UIColor.clear
        return previewParameters
    }

but it only allows me to set the background color not hide the drag preview

and the second way that i tried is by checking the cell state

public override func dragStateDidChange(_ dragState: UICollectionViewCell.DragState) {
    switch dragState {

    case .none:
        self.layer.opacity = 1
    case .lifting:
        self.layer.opacity = 0
    case .dragging:
        self.layer.opacity = 1
    }
}

but it's also not working

do any of you know how to hide this? or atleast hide the border and the shadow is also could solve this problem

here is the lifted cell

enter image description here

Upvotes: 2

Views: 1798

Answers (1)

Benny Wijaya
Benny Wijaya

Reputation: 217

finally i found out the solution, the drag preview actually is named as _UIPlatterView (after debug hierarchy it) and it's child view named as _UIPortalView that block the cell during long press / lifting

and as solution of this post, just subclass the collection view and remove the the child view of _UIPlatterView

How to hide shadows in UITableViewCell when cell is dragging

public class CustomCollectionView: UICollectionView {

    override public func didAddSubview(_ subview: UIView) {
        super.didAddSubview(subview)

        if "\(type(of: subview))" == "_UIPlatterView" {
            subview.subviews.forEach({ $0.removeFromSuperview() })
        }
    }
}

but it's not the end, the above solution still shows the drag preview in just a little second and i add this code to clean it

extension ExampleViewController: UICollectionViewDragDelegate {
    public func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
        guard let currentCell: MUICalendarCollectionViewCell = collectionView.cellForItem(at: indexPath) as? MUICalendarCollectionViewCell else { return nil }

        let previewParameters = UIDragPreviewParameters()
        let path = UIBezierPath(rect: CGRect.zero)
        previewParameters.visiblePath = path
        previewParameters.backgroundColor = MUIColor.clear
        return previewParameters
    }
}

Upvotes: 3

Related Questions