gpt4 book ai didi

ios - UICollectionViewDragDelegate 不工作

转载 作者:行者123 更新时间:2023-12-02 16:28:58 24 4
gpt4 key购买 nike

我成功地实现了一个在屏幕上显示一些虚拟文本的 UICollectionView。一切正常,现在我添加了实现 self.collectionView.dragDelegate = selfUICollectionViewDelegateFlowLayout 的拖动功能,但它不起作用。我还错过了什么吗?

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]
}
}

最佳答案

您需要将此变量 dragInteractionEnabled 设置为 true

 collectionView.dragInteractionEnabled = true

关于ios - UICollectionViewDragDelegate 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63845841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com