ios - 如何更改正在使用 iOS11+ 拖放操作拖动的单元格的背景?

标签 ios swift uicollectionview

我正在尝试在 UICollectionView 中实现新的拖放方法,但不知道如何从被拖动的单元格中删除背景。

这是问题的截图:

enter image description here

这是我正在尝试的代码:

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

问题是,它并没有真正起作用。背景仍然可见。我正在考虑使用 visiblePath: 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
}

enter image description here

然后,委托(delegate)方法:

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
}

结果:

enter image description here

关于ios - 如何更改正在使用 iOS11+ 拖放操作拖动的单元格的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504120/

相关文章:

IOS iPad应用程序底部奇怪的白色区域

ios - 如何在 SharePoint 2013 网站上托管 iOS IPA?

ios - 如何从 FQL 查询访问信息

ios - 使用 UICollectionViewDiffableDataSource 和 NSFetchedResultsController 重新排序单元格

ios - 如何设置动态宽度 UICollectionViewCell

ios - 使用 MAMP 请求超时 iPhone 到 Mac localhost

ios - 如何从中心位置为所有 View Controller 在导航栏中设置 Logo

swift - 在 UITabBarController 上使用 UINavigationItem

ios - 将 UIButton 添加到数组中

ios - 水平 UICollectionView 顶部的奇数空格