问题描述
我正在尝试在 UICollectionView 中实现新的拖放方法,但不知道如何从被拖动的单元格中删除背景.
I'm trying to implement the new drag-n-drop methods in UICollectionView and can't figure out how to remove the background from a cell that's being dragged.
问题截图如下:
这是我正在尝试的代码:
Here's the code I'm trying:
func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
let parameters = UIDragPreviewParameters()
parameters.backgroundColor = .clear
return parameters
}
问题是,它真的不起作用.背景仍然可见.我正在考虑使用 visiblePath: UIBezierPath
,但我认为它不会很好地处理文本.
Problem is, it doesn't really work. The background is still visible. I'm thinking of using visiblePath: UIBezierPath
, but I don't think it's going to play nice with text.
有没有办法完全去除这个背景?
Is there a way to just remove this background completely?
推荐答案
我不确定有没有不使用 UIBezierPath
的方法.
I'm not sure there is a way to do so without using UIBezierPath
.
这是我为类似的东西所做的一个例子:
Here is an example of what I did for something similar:
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
}
结果:
这篇关于如何使用 iOS11+ 拖放更改正在拖动的单元格的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!