ios - 嵌入容器 View 时,UICollectionViewController 重新排序不起作用

标签 ios swift uicollectionview ios9

当我将其添加到我的 UICollectionViewController 子类时,在 iOS9 中可以重新排序

override func collectionView(collectionView: UICollectionView, 
        moveItemAtIndexPath sourceIndexPath: NSIndexPath, 
           toIndexPath destinationIndexPath: NSIndexPath)

当此 UICollectionViewController 子类嵌入到容器 View 中时,它不起作用。

我已经对问题进行了演示 here

关于为什么或如何解决它的任何想法?

最佳答案

Scooter 的答案是正确的! 这是 Swift 3 语法版本:

import UIKit

class ViewController: UIViewController
{
    // MARK: - IBOutlets
    @IBOutlet weak var uiCollectionView: UICollectionView!

    // MARK: - Lifecycle
    override func viewDidLoad()
    {
        super.viewDidLoad()

        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongGesture))
        self.uiCollectionView.addGestureRecognizer(longPressGesture)
    }


    // MARK: - Gesture recogniser
    @objc func handleLongGesture(gesture: UILongPressGestureRecognizer)
    {
        switch(gesture.state)
        {

        case .began:
            guard let selectedIndexPath = self.uiCollectionView.indexPathForItem(at: gesture.location(in: self.uiCollectionView)) else
            {
                break
            }

            self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)

        case .changed:
            self.uiCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))

        case .ended:
            self.uiCollectionView.endInteractiveMovement()

        default:
            self.uiCollectionView.cancelInteractiveMovement()
        }
    }
}


// MARK: - UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        // TODO: Link to your data model
        return 20
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        // TODO: Link to your data model
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
        return cell
    }


    func collectionView(_ collectionView: UICollectionView,
                        moveItemAt sourceIndexPath: IndexPath,
                        to destinationIndexPath: IndexPath)
    {
        // TODO: Update your data model to reflect the change
    }
}


// MARK: - UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate
{
    // TODO: Add any UICollectionViewDelegate here if needed.
}

请注意,此代码不考虑触摸位置偏移 - 因此当您开始拖动时,您的单元格将“跳”到手指下方的中心。如果你想阻止这种情况,那么你需要在你的 UIViewController 中定义一个 CGPoint 属性(在下面的代码中命名为 initialGestureLocationInCell)。然后用这个替换初始示例:

[...]
        case .began:
        guard let selectedIndexPath = self.uiCollectionView.indexPathForItem(at: gesture.location(in: self.uiCollectionView)) else
        {
            break
        }

        let selectedCell = self.uiCollectionView.cellForItem(at: selectedIndexPath)!
        let gestureLocationInCell_RelativeToOrigin = gesture.location(in: selectedCell)
        let gestureLocationInCell_RelativeToCentre = CGPoint(x: gestureLocationInCell_RelativeToOrigin.x - selectedCell.frame.size.width/2,
                                                             y: gestureLocationInCell_RelativeToOrigin.y - selectedCell.frame.size.height/2)

        self.initialGestureLocationInCell = gestureLocationInCell_RelativeToCentre
        self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)

    case .changed:
        let gestureLocationInCollectionView = gesture.location(in: gesture.view!)
        let targetPosition = CGPoint(x: gestureLocationInCollectionView.x - self.initialGestureLocationInCell.x,
                                    y: gestureLocationInCollectionView.y - self.initialGestureLocationInCell.y)

        self.uiCollectionView.updateInteractiveMovementTargetPosition(targetPosition)
[...]

关于ios - 嵌入容器 View 时,UICollectionViewController 重新排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166607/

相关文章:

ios - 如何创建没有数据的新 Firestore 文档?

ios - AudioQueue 修剪和缓冲区大小

swift - 实现游戏对象的特定跳跃行为

ios - 无法使用 AutoLayout 设置 UICollectionViewCell 的宽度

ios - 如何设置 UICollectionView 填充 UIEdgeInsets

ios - iPad 试飞时应用程序未显示

ios - 强制 Xcode 在构建时始终复制资源文件夹

swift - 从另一个 Swift 类访问 navigationItem.title

Swift:在数组中查找 Realm 对象

ios - Collection View 单元格未出现在 Collection View 中?