UICollectionViewDragDelegate没有起作用

3

我成功地实现了一个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]
    }
}
2个回答

8

您需要将变量dragInteractionEnabled设置为true

 collectionView.dragInteractionEnabled = true

1
值得注意的是,在iPhone上,此属性的默认值为false。https://developer.apple.com/documentation/uikit/uicollectionview/2909041-draginteractionenabled - undefined
这在我的情况下很奇怪,因为UICollectionViewDragDelegate对于iOS 12和iOS 15可以工作,而不需要collectionView.dragInteractionEnabled = true。但是,在iOS 14中需要显式启用。 - undefined

0
确保在`collectionView(_:itemsForBeginning:at:)`函数中返回一些内容。对于UITableView,返回一个空数组是可以的,但对于UICollectionView则不行。
另外,请检查从`collectionView(_:canHandle:)`函数中返回的内容。如果返回false,将阻止拖动操作的发生。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接