Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Pan Gesture to UICollectionView Cell - IOS/Swift

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.

like image 920
GMHSJ Avatar asked Dec 11 '25 13:12

GMHSJ


1 Answers

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)
    ...
}
like image 131
Pedro Martins Avatar answered Dec 13 '25 02:12

Pedro Martins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!