我成功地实现了UICollectionView
在屏幕上显示一些虚拟文本。一切正常,现在我添加了拖动功能实现self.collectionView.dragDelegate = self
和UICollectionViewDelegateFlowLayout
,但它不起作用。我还缺少什么吗?
final class XptoComponent: UIView {
private var titles = ["Xpto 1", "Xpto 2", "Xpto 3", "Xpto 4", "Xpto 5", "Xpto 6", "Xpto 7", "Xpto 8", "Xpto 9"]
private var collectionView: UICollectionView = {
let collectionViewLayout = UICollectionViewFlowLayout()
collectionViewLayout.minimumInteritemSpacing = 0
collectionViewLayout.minimumLineSpacing = 0
collectionViewLayout.itemSize = CGSize(width: UIScreen.main.bounds.size.width, height: NumericConstant.ViewCell.height)
collectionViewLayout.sectionInset = .zero
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout).usingAutoLayout()
collectionView.registerCell(XptoCollectionViewCell.self)
return collectionView
}()
init() {
super.init(frame: .zero)
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.dragDelegate = self
self.defineSubviews()
self.defineSubviewsConstraints()
}
private func defineSubviewsConstraints() {
NSLayoutConstraint.activate([
self.collectionView.topAnchor.constraint(equalTo: self.topAnchor),
self.collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
self.collectionView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor)
])
}
private func defineSubviews() {
self.addSubview(self.collectionView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension XptoComponent: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.titles.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(XptoCollectionViewCell.self, for: indexPath)
cell?.titleLabel.text = self.titles[indexPath.row]
return cell ?? UICollectionViewCell()
}
}
extension XptoComponent: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width, height: 100)
}
}
extension XptoComponent: UICollectionViewDragDelegate {
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let title = self.titles[indexPath.item]
let itemProvider = NSItemProvider(object: title as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
return [dragItem]
}
}