【问题标题】:Drag and drop between cells in CollectionView doesn't work, iOS, PanGestureRecognizer and Swift3在 CollectionView 中的单元格之间拖放不起作用,iOS、PanGestureRecognizer 和 Swift3
【发布时间】:2017-05-28 18:56:14
【问题描述】:

我尝试创建包含 2 行和 2 列的集合视图,并在单元格之间进行简单的拖放。我将发布我的控制器的 collectionView() 和 panGestureAction() 的样子。 当我将手势识别器添加到故事中时,我已将其放在可重复使用的单元格上。

问题是当我尝试拖放时,只有最后一个单元格可以引发事件,并且我看到控制台日志(“开始”和“结束”)。 如果我注释行 cell.contentView.addGestureRecognizer(recognizer),则只有第一个单元格会引发事件。

我应该怎么做,我的所有单元格都在记录消息(能够识别拖放)?

@IBOutlet weak var recognizer: UIPanGestureRecognizer!

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> 
 UICollectionViewCell {

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReusable, for: indexPath) as? CellCollectionViewCell else {
        fatalError("The dequeued cell is not an instance of CellCollectionViewCell.")
    }

    cell.contentView.addGestureRecognizer(recognizer)

    // Configure the cell
    cell.valueLabel?.text = String(100);
    cell.layer.borderWidth = 5.0
    cell.layer.borderColor = UIColor.red.cgColor

    cells.append(cell);

    return cells[cells.count-1];
}


 @IBAction func panGestureAction(_ sender: UIPanGestureRecognizer) {
    if recognizer.state == UIGestureRecognizerState.began {
        print("began");
    }

    if recognizer.state == UIGestureRecognizerState.ended {
        print("ended");
    }
}

更新:

import UIKit

class GridControllerCollectionViewController: UICollectionViewController, UIGestureRecognizerDelegate {

private let cellReusable = "CellCollectionViewCell"

fileprivate let numberOfColumns = 2
fileprivate let numberOfRows = 4
fileprivate var gesture: UILongPressGestureRecognizer?

@IBOutlet var collectionViewOutlet: UICollectionView!

fileprivate var cells = [CellCollectionViewCell]()

override func viewDidLoad() {
    super.viewDidLoad()

    gesture = UILongPressGestureRecognizer(target: self, action: #selector(panGestureAction(_:)))
    gesture?.minimumPressDuration = 0.5
    gesture?.delegate = self

    //have tried with first line, second, and with both
    collectionViewOutlet.addGestureRecognizer(gesture!)
    collectionView?.addGestureRecognizer(gesture!)

    print("gesture delegate is assigned!")
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return numberOfRows
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return numberOfColumns
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReusable, for: indexPath) as? CellCollectionViewCell else {
        fatalError("The dequeued cell is not an instance of CellCollectionViewCell.")
    }

    // Configure the cell
    cell.valueLabel?.text = String(100);
    cell.layer.borderWidth = 5.0
    cell.layer.borderColor = UIColor.red.cgColor

    cells.append(cell);

    return cells[cells.count-1];
}

func panGestureAction(_ sender: UILongPressGestureRecognizer) {

    print("panGestureAction was reached!");

//        if gesture?.state == UIGestureRecognizerState.began {
//            print("began");
//        }
//        
//        if gesture?.state == UIGestureRecognizerState.ended {
//            print("ended");
//        }

    switch sender.state {
    case .began:
        print("began")
    case .changed:
        print("changed")
    case .ended:
        print("ended")
    default:
        collectionView?.cancelInteractiveMovement()
    }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return true
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive press: UIPress) -> Bool {
    return true
}

override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
    return true
}
}

【问题讨论】:

    标签: ios swift3 drag-and-drop uicollectionview uipangesturerecognizer


    【解决方案1】:

    不要将手势添加到单元格中。必须添加到您的collectionView

    gesture = UILongPressGestureRecognizer(target: self, action: #selector(panGestureAction(_:)))
    gesture.minimumPressDuration = 0.5
    gesture.delegate = self
    collectionView.addGestureRecognizer(gesture)
    

    实现手势委托方法

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return true
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive press: UIPress) -> Bool {
        return true
    }
    

    重要提示:实现集合视图数据源方法

    func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        //
    }
    

    最后,实现你的平移手势动作

    func panGestureAction(_ sender: UILongPressGestureRecognizer) {
        switch sender.state {
        case .began:
            guard let indexPath = collectionView.indexPathForItem(at: sender.location(in: collectionView)) else {
                return
            }
            collectionView.beginInteractiveMovementForItem(at: indexPath)        
        case .changed:
            collectionView.updateInteractiveMovementTargetPosition(sender.location(in: collectionView))
        case .ended:
            collectionView.endInteractiveMovement()
        default:
            collectionView.cancelInteractiveMovement()
        }
    }
    

    【讨论】:

    • 感谢您的帮助!不幸的是,该解决方案没有奏效。我将您的台词放入 viewDidLoad() 并删除了我以前的手势识别器。我还必须确认 UIGestureRecognizerDelegate 协议,因为添加了 gesture.delegate = self ,并将手势定义为类的私有属性。现在对拖放没有任何反应。
    • 我已经发布了我的 panGestureAction,但没有任何内容。 func panGestureAction(_ sender: Any) { print("here"); }
    • 再次感谢您的详细解释。不幸的是,我又一次没有成功。我已经用 CollectionViewController 的实际状态更新了我的第一篇文章。
    • 你错过了这一行collectionView.addGestureRecognizer(gesture)
    • 再次感谢!!!还是没做到。新更新://已尝试使用第一行、第二行以及 collectionViewOutlet.addGestureRecognizer(gesture!) collectionView?.addGestureRecognizer(gesture!)
    猜你喜欢
    • 1970-01-01
    • 2014-08-02
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多