Swift 5: 未在 UICollectionView 中调用 scrollViewDidScroll class

Swift 5: scrollViewDidScroll not called in UICollectionView class

是的,我已经检查了有关此主题的其他问题。由于某种原因,函数

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print(scrollView.contentOffset)
}

未在我的 UICollectionView 中调用 class。

这是完整的 class。当用户在每个单元格上滑动时,我基本上想更新我的 pageControl。 我尝试了 collectionView:willDisplayCell:forItemAtIndexPath: 方法,但我发现有时当我滚动并在单元格中途停止并返回时,它不会更新到以前的值。因此,为什么我求助于滚动视图委托。

class FilterInfoImagesCollectionViewController: UICollectionViewController {

    var selectedFilterCollection: FilterCollection!
    private let pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.currentPage = 0
        pageControl.numberOfPages = 5 // Hardcoded since we know there will only be 5 example images
        pageControl.translatesAutoresizingMaskIntoConstraints = false
        return pageControl
    }()
    private var datasource: DataSource!
    private var datas = [FilterInfo]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.alwaysBounceVertical = false
        collectionView.isPagingEnabled = true
        collectionView.register(FilterInfoCollectionViewCell.self, forCellWithReuseIdentifier: FilterInfoCollectionViewCell.ReuseIdentifier)
        collectionView.collectionViewLayout = createLayout()
        collectionView.backgroundColor = .clear
        
        collectionView.delegate = self
        collectionView.dataSource = self

        configureDatasource()
        setupDatas()
        
        setupView()
        setupConstraint()
    }
    
    private func setupDatas() {
        let filterName = selectedFilterCollection.name.components(separatedBy: " ").first!
        for n in 1 ... 5 {
            let image = UIImage(named: "\(filterName).ex\(n)")
            
            let filterInfo = FilterInfo(image: image, filterName: "\(filterName)\(n)")
            datas.append(filterInfo)
        }
        
        self.createSnapshot(from: datas)
    }
    
    private func setupView() {
        self.view.addSubview(pageControl)
    }
    
    private func setupConstraint() {
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
}

extension FilterInfoImagesCollectionViewController {
    fileprivate enum Section { case main }
    fileprivate typealias DataSource = UICollectionViewDiffableDataSource<Section, FilterInfo>
    fileprivate typealias Snapshot = NSDiffableDataSourceSnapshot<Section, FilterInfo>
    
    
    fileprivate func configureDatasource() {
        datasource = DataSource(collectionView: collectionView!, cellProvider: { (collectionView, indexPath, filterInfo) -> UICollectionViewCell? in
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FilterInfoCollectionViewCell.ReuseIdentifier, for: indexPath) as? FilterInfoCollectionViewCell else { return nil }
            
            cell.filterInfo = filterInfo
            
            return cell
        })
    }
    
    fileprivate func createSnapshot(from datas: [FilterInfo]) {
        var snapshot = Snapshot()
        snapshot.appendSections([.main])
        snapshot.appendItems(datas)
        datasource.apply(snapshot, animatingDifferences: true)
    }
    
    private func createLayout() -> UICollectionViewLayout {
        
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        let groupSize = NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1),
            heightDimension: .fractionalHeight(1))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 1)
        
        let section = NSCollectionLayoutSection(group: group)
        section.orthogonalScrollingBehavior = .groupPaging
        
        let layout = UICollectionViewCompositionalLayout(section: section)
        return layout
    }
}

extension FilterInfoImagesCollectionViewController {
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset)
    }
}

怎么不叫呢?我尝试检查其他 scrollview 委托方法,同样的事情发生了 - 没有被调用。

更新:根据请求向自身添加委托和数据源

添加到viewDidLoad。与 UICollectionViewDelegate 相关的滚动功能,您需要设置委托以使用您的覆盖功能

section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
    self?.pager.currentPage = visibleItems.last!.indexPath.row
}