【问题标题】:Hide UICollectionViewCell Drag Preview隐藏 UICollectionViewCell 拖动预览
【发布时间】:2020-05-26 12:49:36
【问题描述】:

我正在使用UICollectionViewDragDelegate 在集合视图中实现拖放,并尝试在拖动时隐藏拖动预览

在关注此线程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
    }
}

但它也不起作用

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

这里是提升的单元格

【问题讨论】:

  • 如何隐藏我拖动的collectionView单元格谁能帮忙而不是拖动预览,我尝试使用dragStateDidChange解决,但是当我尝试解决它时其他单元格开始行为不端?

标签: ios uicollectionview drag-and-drop


【解决方案1】:

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

作为这篇文章的解决方案,只需将集合视图子类化并删除_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() })
        }
    }
}

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

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

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2020-07-21
    • 2012-08-16
    相关资源
    最近更新 更多