【发布时间】: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