簡體   English   中英

如何使用 iOS11+ 拖放更改正在拖動的單元格的背景?

[英]How to change background of a cell that's being drag with iOS11+ drag-n-drop?

我正在嘗試在 UICollectionView 中實現新的拖放方法,但無法弄清楚如何從被拖動的單元格中刪除背景。

這是問題的屏幕截圖:

在此處輸入圖片說明

這是我正在嘗試的代碼:

func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
    let parameters = UIDragPreviewParameters()
    parameters.backgroundColor = .clear
    return parameters
}

問題是,它真的不起作用。 背景仍然可見。 我正在考慮使用visiblePath: UIBezierPath ,但我認為它不會很好地處理文本。

有沒有辦法完全刪除這個背景?

我不確定有沒有辦法在不使用UIBezierPath情況下做到這UIBezierPath

這是我為類似的事情所做的一個例子:

final class CustomCell: UITableViewCell {
    @IBOutlet weak var mySuperLabel: UILabel!
    @IBOutlet weak var whiteSubview: UIView! // I have added a cornerRadius of 12.0 in the XIB
}

在此處輸入圖片說明

然后,委托方法:

func tableView(_ tableView: UITableView, dragPreviewParametersForRowAt indexPath: IndexPath) -> UIDragPreviewParameters? {
    let cell = tableView.cellForRow(at: indexPath) as! CustomCell
    let previewParameters = UIDragPreviewParameters()
    let path = UIBezierPath(roundedRect: cell.whiteSubview.frame, cornerRadius: 12.0)
    previewParameters.visiblePath = path
    previewParameters.backgroundColor = .clear
    return previewParameters
}

結果:

在此處輸入圖片說明

另一種方法是在拖動開始時使用 ItemProvider。

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    let itemProvider = NSItemProvider()
    let dragItem = UIDragItem(itemProvider: itemProvider)
    dragItem.previewProvider = { () -> UIDragPreview? in
        let cell = collectionView.cellForItem(at: indexPath) // Do cell preview modifications here
        cell?.layer.backgroundColor = UIColor.clear.cgColor
        cell?.backgroundColor = .clear
        return UIDragPreview(view: cell!)
    }
    return [dragItem]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM