ios 检测 UICollectionView 中的页面变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29507675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:34:11  来源:igfitidea点击:

Detect page change in UICollectionView

iosobjective-ciphoneios8

提问by Ahsan Ebrahim

I tried finding this question for a while but could not find this problem's answer. My problem is that i have a UICollectionViewand the Scroll Direction is Horizontalwith 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 intvariable and now want to add or subtract it by 1 each time the user swipes right or left. I tried using scrollView's delegate

我试图找到这个问题一段时间,但找不到这个问题的答案。我的问题是,我有一个UICollectionView和滚动方向是HorizontalPaging Enabled。我的问题是我想保留用户所在的当前页码的粘性,所以我创建了一个int变量,现在每次用户向右或向左滑动时都想加或减 1。我尝试使用scrollView的委托

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

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 UICollectionViewplus it wont let me know that whether the user went to the next page or the previous one.

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

回答by itsji10dra

Use :

用 :

- (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);
}

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

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

回答by Ahmed Safadi

Swift 3 Xcode 8.2

斯威夫特 3 Xcode 8.2

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let x = scrollView.contentOffset.x
        let w = scrollView.bounds.size.width
        let currentPage = Int(ceil(x/w))
        // Do whatever with currentPage.
}

回答by Andrej

Based on @Shankar BS 's answer I've implemented it like this in Swit. Keep in mind that CollectionViewDelegate conforms to ScrollViewDelegate:

基于@Shankar BS 的回答,我已经在 Swit 中实现了它。请记住,CollectionViewDelegate 符合 ScrollViewDelegate:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
    print("page = \(page)")
}

回答by Shankar BS

You can get the current page like below, index will be from 0to (total page - 1)

您可以获得如下所示的当前页面,索引将从0(total page - 1)

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    NSLog(@"Current page -> %d",page);
}

回答by Jonni ?kesson

Swift 2.2

斯威夫特 2.2

 func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let x = collectionView.contentOffset.x
    let w = collectionView.bounds.size.width
    let currentPage = Int(ceil(x/w))
    print("Current Page: \(currentPage)")
}

回答by álvaro Agüero

SWIFT 5

快速 5

For example, put this method in your ViewController with extensions UICollectionViewDelegate,UICollectionViewDataSource

例如,将此方法放在带有扩展 UICollectionViewDelegate,UICollectionViewDataSource 的 ViewController 中

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let pageWidth = scrollView.frame.size.width
        let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
        print("page = \(page)")
    }

回答by wcoder

Xamarin.iOS:

Xamarin.iOS:

You can override DecelerationEndedmethod of UICollectionViewDelegateor subscribe to DecelerationEndedevent of UICollectionView(custom delegate will be used under the hood).

您可以覆盖DecelerationEnded方法UICollectionViewDelegate或订阅DecelerationEnded事件UICollectionView(自定义委托将在后台使用)。

public override void DecelerationEnded(UIScrollView scrollView)
{
    var x = scrollView.ContentOffset.X;
    var w = scrollView.Bounds.Size.Width;
    var currentPage = Math.Ceiling(x / w);

    // use currentPage here
}