在UICollectionView中检测页面更改 [英] Detect page change in UICollectionView

查看:87
本文介绍了在UICollectionView中检测页面更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试找到该问题已有一段时间,但找不到该问题的答案. 我的问题是我有一个UICollectionView,滚动方向是HorizontalPaging Enabled.我的问题是我想保持用户当前页面编号的固定性,因此我创建了一个int变量,现在想在用户每次向右或向左滑动时将其加减1.我尝试使用scrollView的委托

I tried finding this question for a while but could not find this problem's answer. My problem is that i have a UICollectionView and the Scroll Direction is Horizontal with Paging Enabled. My problem is that i want to keep the tack of the current page number on which the user is, so i created an int variable and now want to add or subtract it by 1 each time the user swipes right or left. I tried using scrollView's delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

,但是当用户向右或向左滑动时,它被调用的次数与UICollectionView中页面的列数一样多,并且它不会让我知道用户是转到下一页还是转到上一个.

but when the user swipes right or left, it is called as many number of the times as the number of columns on a page in the UICollectionView plus it wont let me know that whether the user went to the next page or the previous one.

推荐答案

使用情况:

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

    if (0.0f != fmodf(currentPage, 1.0f))
    {
        pageControl.currentPage = currentPage + 1;
    }
    else
    {
        pageControl.currentPage = currentPage;
    }

    NSLog(@"Page Number : %ld", (long)pageControl.currentPage);
}

如果您不使用任何pageControl,则ceil(currentPage)将是您当前的页码.

And if you are not using any pageControl, then ceil(currentPage) will be your current page number.

这篇关于在UICollectionView中检测页面更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆