UICollectionView drag and drop remove translucent cell

Viewed 1054

I'm using iOS 11 drag and drop API for reordering and want to remove translucent cell which appears on start dragging. Is it possible? For dragging I use only required method of UICollectionViewDragDelegate:

- (nonnull NSArray<UIDragItem *> *)collectionView:(nonnull UICollectionView *)collectionView
                     itemsForBeginningDragSession:(nonnull id<UIDragSession>)session
                                      atIndexPath:(nonnull NSIndexPath *)indexPath {
    NSItemProvider *itemProvider = [NSItemProvider new];
    UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:itemProvider];

    return @[dragItem];
}

enter image description here

2 Answers

You can customize your cell during the drag/drop lifecycle by overriding dragStateDidChange(_ dragState: UICollectionViewCell.DragState)

class MyCell: UICollectionViewCell {

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

        switch dragState {
        case .none:
            self.layer.opacity = 1
        case .lifting:
            return
        case .dragging:
            self.layer.opacity = 0

        }
    }
}

On your cell class override this method:

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

    switch dragState {
    case .none:

        self.layer.opacity = 1

    case .lifting:

        return

    case .dragging:

        self.layer.opacity = 0
    }
}
Related