iOS6 UICollectionView and UIPageControl - How to get visible cell?

后端 未结 7 2114
甜味超标
甜味超标 2020-12-02 12:37

While studying iOS6 new features I got a question about UICollectionView.
I am currently testing it with Flow layout and the scroll direction set to horizontal, scrolli

7条回答
  •  星月不相逢
    2020-12-02 12:58

    You must setup yourself as UIScrollViewDelegate and implement the scrollViewDidEndDecelerating:method like so:

    Objective-C

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        CGFloat pageWidth = self.collectionView.frame.size.width;
        self.pageControl.currentPage = self.collectionView.contentOffset.x / pageWidth;
    }
    

    Swift

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    
        let pageWidth = self.collectionView.frame.size.width
        pageControl.currentPage = Int(self.collectionView.contentOffset.x / pageWidth)
    }
    

提交回复
热议问题