User17373928
User17373928

Reputation: 29

How to use UIScrollViewDelegate in Custom CollectionView?

I’m making a custom collection view for my app.
I want to call scrollViewWillEndDragging method when I scroll CustomCollectionView on ViewController.

When it drew CustomCollectionView on screen, CommonInit was called but scrollViewWillEndDragging was not called when I scrolled it.

How can I call it?

Here is my code.

in CustomView

class CustomView: UICollectionView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() {
    …
   }
}

extension CustomView: UIScrollViewDelegate {

// want to call this method
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
    {
}

in AViewController

class AViewController: UIViewController {
    @IBOutlet weak var collectionView: CustomView!
    …
    override func viewDidLoad() {
    …
    collectionView.register(UINib(nibName: "Cell", bundle: nil), forCellWithReuseIdentifier: "Cell")
    …
    }
}

Upvotes: 1

Views: 294

Answers (1)

You should set the collectionView delegate. You can do it in your CustomView in commonInit like :

delegate = self

Edit

And you should change UIScrollViewDelegate to UICollectionViewDelegate, UICollectionViewDelegate inherite from UIScrollViewDelegate

Upvotes: 2

Related Questions