问题描述
我正在成功实现一个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]
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)