iOS6 UICollectionView and UIPageControl - How to get visible cell?

后端 未结 7 2076
甜味超标
甜味超标 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 13:01

    1. Place PageControl in your view or set by Code.
    2. Set UIScrollViewDelegate
    3. In Collectionview-> cellForItemAtIndexPath (Method) add the below code for calculate the Number of pages,

    int pages =floor(ImageCollectionView.contentSize.width/ImageCollectionView.frame.size.width); [pageControl setNumberOfPages:pages];

    Add the ScrollView Delegate method,

    pragma mark - UIScrollVewDelegate for UIPageControl

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        CGFloat pageWidth = ImageCollectionView.frame.size.width;
        float currentPage = ImageCollectionView.contentOffset.x / pageWidth;
    
        if (0.0f != fmodf(currentPage, 1.0f))
        {
            pageControl.currentPage = currentPage + 1;
        }
        else
        {
            pageControl.currentPage = currentPage;
        }
        NSLog(@"finishPage: %ld", (long)pageControl.currentPage);
    }
    

提交回复
热议问题