Kirill Kirill - 1 month ago 20
Swift Question

Detect scrolling control

I have 2 ViewCollection at ViewController and function

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

}


How can I detect which collection scrolling in current function ?

Answer

UICollectionView is just a subclass of UIScrollView. Just keep reference to your collectionViews, and you can check if the scrollview and the collectionview is equal.

So simply use the following code:

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        if scrollView == self.collectionViewA {
            // do something with collectionViewA
        } else if scrollView == self.collectionViewB {
            // do something with collectionViewB
        } else {
            // unknown collectionView
        }
    }