我尝试创建集合视图,包含2行和2列,并在单元格之间进行简单的拖放操作。我将发布我的控制器的collectionView()和panGestureAction()的样子。 当我在故事中添加手势识别器时,我已将其丢弃在可重复使用的单元格上。
问题是当我尝试拖放时,只有最后一个单元格可以抛出一个事件,我看到控制台日志(“开始”和“结束”)。 如果我评论 cell.contentView.addGestureRecognizer(识别器)行,则只有第一个单元格会抛出一个事件。
我该怎么做,我的所有单元格都在记录消息(能够识别拖放)?
@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
}
}
答案 0 :(得分:2)
不要将手势添加到单元格。必须将其添加到您的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()
}
}