如何在自定义 CollectionView 中使用 UIScrollViewDelegate?

How to use UIScrollViewDelegate in Custom CollectionView?

我正在为我的应用制作自定义集合视图。
我想在 ViewController.

上滚动 CustomCollectionView 时调用 scrollViewWillEndDragging 方法

当它在屏幕上绘制 CustomCollectionView 时,调用了 CommonInit,但在我滚动它时没有调用 scrollViewWillEndDragging。

如何调用?

这是我的代码。

在自定义视图中

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>)
    {
}

AViewController

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

您应该设置 collectionView 委托。您可以在 commonInit 中的 CustomView 中执行此操作,例如:

delegate = self

Edit

您应该将 UIScrollViewDelegate 更改为 UICollectionViewDelegateUICollectionViewDelegate 继承自 UIScrollViewDelegate