Tim*_*imD 5 uicollectionview uicollectionviewdragdelegate
我敢肯定,一定是这样做的一个简单的方法,但我花了很长一段时间下来没有成功的各种兔子洞为止。
我有一个支持拖放的集合视图。被拖动的细胞具有UIImageView在contentView,并且图像视图的衬里层具有施加一个拐角半径。单元格中所有视图的背景颜色都是清晰的。
拖动时,单元格有一个白色背景,显示在图像视图的角落周围:
有没有办法四舍五入整个可拖动视图;或将其背景设置为清除以便不可见恼人的白色边框?
更新
事实证明,解决方案非常简单(假设UIBezierPaths符合您对简单的定义):
您需要覆盖协议的collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath)方法UICollectionViewDragDelegate,并返回 UIDragPreviewParameters适当的 UIBezierPath 集:
func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
let previewParams = UIDragPreviewParameters()
let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 140, height: 140), cornerRadius: 20)
previewParams.visiblePath = path
return previewParams
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的实现,它硬编码了CGRect贝塞尔路径的来源 - 这适用于我的场景,因为所有单元格的大小都相同。更复杂的集合视图在这里需要一些自定义计算。
我认为这比那更容易。如果您的单元格已经具有带有圆形 UIImageView 的清晰背景,则只需设置backgroundColor为..clearUIDragPreviewParameters
func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
let params = UIDragPreviewParameters()
params.backgroundColor = .clear
return params
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据要求,这是单元格的精简版本
final class MyCell: UICollectionViewCell {
// MARK: Constants
private enum Constants {
static let cornerRadius = CGFloat(8)
}
// MARK: Views
private let imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = Constants.cornerRadius
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
// MARK: Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
override func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil
}
// MARK: Public
func update(with image: UIImage) {
imageView.image = image
}
// MARK: Private
private func setup() {
contentView.backgroundColor = .clear
contentView.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
357 次 |
| 最近记录: |