3

I have an UICollectionView and I want to add pan gesture to its Cells/Items. When I add the gesture in usual way UICollectionView is not getting scrolled.

This is how I add the gesture

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;
}

Is there something wrong here? Can someone please tell me a way to get my work done. Any help would be highly appreciated.

Hanushka Suren
  • 723
  • 3
  • 10
  • 32
  • check this answer i hope it help you :) [Swift gesture](http://stackoverflow.com/a/18848817/6628878) – MedAmine.Rihane Aug 11 '16 at 17:50
  • Try extending your collection view object, from there implement touchesbegan touch events method. And then handle your pan event, and then implement [super touchevents] method, so that the existing collectionview scroll will not affect. – Bharath Aug 12 '16 at 07:51

2 Answers2

1

You should add the gesture to the collection view and not to the cell itself. Something like...

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

func handlePanGesture(gesture: UIPanGestureRecognizer) {

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

Just a proposal, I didn't test it:

Create a custom cell:

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

and you can use the delegation to inform CaptureViewController.

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49