alionthego
alionthego

Reputation: 9773

How do I enable collectionView paging over a panGestureRecognizer

I currently have a panGesture recognizer attached to a collectionView that also has paging enabled.

The problem is the panGesture is overriding the paging. I want the paging to be actioned before the panGesture if that's possible.

I'm use the panGesture to move the collectionView vertically and paging to swipe through the cells horizontally.

Upvotes: 0

Views: 149

Answers (2)

alionthego
alionthego

Reputation: 9773

What I ended up doing was iterating through the collectionView's gestureRecognizers:

if let gestureRecognizers = collectionView.gestureRecognizers {
    for gestureRecognizer in gestureRecognizers {
         print(gestureRecognizer)
    }
}

By printing each one and reading the description it was apparent the paging was done by the gestureRecognizer at index 1 (UIScrollViewPagingSwipeGestureRecognizer)

then I used:

myPanGesture.require(toFail: collectionView.gestureRecognizers![1])

It works but with some lag in my use. I imagine this might be helpful in other cases though so I'm posting my solution.

Upvotes: 0

Nilomi Shah
Nilomi Shah

Reputation: 186

we can achieve same functionality, without PanGesture , by detecting scroll of collectionview.

func scrollViewDidScroll(_ scrollView: UIScrollView) {         
 let visibleRect = CGRect(origin: collectionViewCreateNewGame.contentOffset, size: collectionViewCreateNewGame.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let indexPath = collectionViewCreateNewGame.indexPathForItem(at: visiblePoint)
pageControl.currentPage = (indexPath?.row)!
}

Upvotes: 0

Related Questions