How to disbale scrolling in nscollectionview using swift on some specific condition

Viewed 561

I have an NSCollectionView and have to disable its scrollview scrolling property so that it doesn't get reloaded on some specific condition and show user an alert if he tries to scroll.

Any delegate method which is called when a view is scrolled which can restrict scrolling and reloading of collection view data would help.

Thanks in advance.

2 Answers

Subclass NSCollectionView and override scrollWheel

import Cocoa

class YourCollectionView: NSCollectionView {
    var disableScroll = true

    override func scrollWheel(with event: NSEvent) {
        if disableScroll {
            return
        } else {
            super.scrollWheel(with: event)
        }
    }
}

You can do with the help of scrollViewDidScroll method of ScrollViewDelegate.You can use this method because collectionview is subclass of scrollview.

For Example :

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("collectionView Content : \(collectionView.contentOffset.y)")

            if collectionView.contentOffset.y >= 100
            {
                collectionView.isScrollEnabled = false
            }
            else
            {
                collectionView.isScrollEnabled = true
            }

    }

I hope its work for you.

Related