ios - 隐藏 UICollectionViewCell 拖动预览

标签 ios uicollectionview drag-and-drop

我正在使用 UICollectionViewDragDelegate 在 Collection View 中实现拖放,并尝试在拖动时隐藏拖动预览

在关注此线程后,我设法使用这行代码来隐藏它 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
        }
}

但是在抬起时拖动预览仍然显示,并且允许我在抬起过程中修改拖动预览的唯一方法是

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
    }

但它只允许我设置背景颜色而不能隐藏拖动预览

我尝试的第二种方法是检查单元格状态

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
    }
}

但它也不起作用

你们知道如何隐藏它吗?或者至少隐藏边框和阴影也可以解决这个问题

这是抬起的单元格

enter image description here

最佳答案

最后我找到了解决方案,拖动预览实际上被命名为_UIPlatterView(在调试层次结构之后),它的 subview 名为_UIPortalView,在期间阻止单元格长按/抬起

作为本文的解决方案,只需对 Collection View 进行子类化并删除 _UIPlatterView

的 subview 即可

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() })
        }
    }
}

但这还没有结束,上面的解决方案仍然在短短一秒钟内显示拖动预览,我添加了这段代码来清理它

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
    }
}

关于ios - 隐藏 UICollectionViewCell 拖动预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022540/

相关文章:

javascript - 拖放图像并以 Angular 2 进行图像预览

ios - iOS 中的自定义逐帧动画

ios - 如何实现 [example.text intValue]?

ios - 设置变量后 UICollectionView 为 nil

ios - 单元格包含 UITableViews 的 UICollectionView

java - Android拖放多个 TextView

javascript - CSS/HTML5 :hover state remains after drag and drop

iOS - 在类型为 'isConnected' 的对象上找不到属性 'CBPeripheral'

ios - Swift 3 中的通知中心崩溃

ios - 以编程方式定义的约束 Swift iOS