首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏UICollectionViewCell拖动预览

隐藏UICollectionViewCell拖动预览
EN

Stack Overflow用户
提问于 2020-05-26 20:49:36
回答 1查看 631关注 0票数 2

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

在关注了这个线程Custom View for UICollectionViewCell Drag Preview之后,我设法用这行代码隐藏了它

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

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

代码语言:javascript
复制
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
    }

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

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

代码语言:javascript
复制
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
    }
}

但它也不起作用

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

这是被抬起来的牢房

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-27 23:28:41

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

作为这篇文章的解决方案,只需子类化集合视图并删除_UIPlatterView的子视图

How to hide shadows in UITableViewCell when cell is dragging

代码语言:javascript
复制
public class CustomCollectionView: UICollectionView {

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

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

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

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62022540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档