を使用してコレクションビューにドラッグアンドドロップを実装しUICollectionViewDragDelegate
、ドラッグ中にドラッグプレビューを非表示にしようとしました
このスレッドに従って、このコード行を使用して非表示にすることができますUICollectionViewCellドラッグプレビューのカスタムビュー:
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
}
ただし、ドラッグプレビューを非表示にせず、背景色を設定することしかできません。
私が試した2番目の方法は、セルの状態を確認することです。
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
}
}
しかし、それも機能していません
これを隠す方法を知っている人はいますか?または少なくとも境界線を非表示にし、影もこの問題を解決できる可能性があります
これが持ち上げられたセルです
ついに私は解決策を見つけました、ドラッグプレビューは実際には_UIPlatterView
(デバッグ階層の後に)という名前が付けられ、_UIPortalView
長押し/持ち上げ中にセルをブロックするという名前の子ビューです
この投稿の解決策として、コレクションビューをサブクラス化し、の子ビューを削除するだけです。 _UIPlatterView
セルがドラッグしているときにUITableViewCellで影を非表示にする方法
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
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加