将平移手势添加到UICollectionView单元 - IOS/Swift
问题描述:
我有一个UICollectionView,我想将平移手势添加到它的Cells/Items。当我以通常的方式添加手势UICollectionView没有滚动。将平移手势添加到UICollectionView单元 - IOS/Swift
这是我的错在这里添加手势
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(CaptureViewController.pagesCollectionViewItemPanEvent(_:)))
cell.addGestureRecognizer(panGesture)
return cell;
}
有什么事?有人可以告诉我一种方法来完成我的工作。任何帮助将不胜感激。
答
只是一个建议,我没有测试:
创建一个自定义单元格:
class PanCell: UICollectionViewCell {
override func awakeFromNib() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.pagesCollectionViewItemPanEvent(_:)))
self.addGestureRecognizer(panGesture)
}
}
,您可以使用代表团向CaptureViewController
。
答
您应该将手势添加到集合视图而不是单元本身。 喜欢的东西...
let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
collectionView.addGestureRecognizer(panGesture)
func handlePanGesture(gesture: UIPanGestureRecognizer) {
let locationInView = gesture.locationInView(collectionView)
...
}
检查这个答案我希望它帮你:) [斯威夫特的手势(http://stackoverflow.com/a/18848817/6628878) –
尝试扩展您的收藏视图对象从,有实现触摸开始触摸事件的方法。然后处理您的pan事件,然后实现[super touchevents]方法,以便现有的collectionview滚动不会产生影响。 – Bharath