将平移手势添加到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; 
} 

有什么事?有人可以告诉我一种方法来完成我的工作。任何帮助将不胜感激。

+0

检查这个答案我希望它帮你:) [斯威夫特的手势(http://*.com/a/18848817/6628878) –

+0

尝试扩展您的收藏视图对象从,有实现触摸开始触摸事件的方法。然后处理您的pan事件,然后实现[super touchevents]方法,以便现有的collectionview滚动不会产生影响。 – Bharath

只是一个建议,我没有测试:

创建一个自定义单元格:

class PanCell: UICollectionViewCell { 
    override func awakeFromNib() { 
     let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.pagesCollectionViewItemPanEvent(_:))) 
     self.addGestureRecognizer(panGesture) 
    } 
} 

,您可以使用代表团向CaptureViewController

+0

为单元格创建自定义类将解决我的问题?对不起,我没有得到这个。你能解释一下:) – GMHSJ

+0

创建一个自定义的单元格,你可以管理单元格内的手势,我不知道是否要解决你的问题,但我会帮助你区分类的行为,这是一个很好的开始点你的问题。 –

+0

好的明白了。将尝试它。谢谢bro – GMHSJ

您应该将手势添加到集合视图而不是单元本身。 喜欢的东西...

let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:") 
collectionView.addGestureRecognizer(panGesture) 

func handlePanGesture(gesture: UIPanGestureRecognizer) { 

    let locationInView = gesture.locationInView(collectionView) 
    ... 
}