我正在使用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
}
}但它也不起作用
你们有人知道怎么隐藏这个吗?或者至少隐藏边框和阴影也可以解决这个问题
这是被抬起来的牢房

发布于 2020-05-27 23:28:41
最后我找到了解决方案,拖动预览实际上被命名为_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
}
}https://stackoverflow.com/questions/62022540
复制相似问题