ios 获取当前页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4132993/
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-30 18:03:00  来源:igfitidea点击:

Getting the current page

iosobjective-ciphonecocoa-touchuiscrollview

提问by AlexVogel

In my scroll view, I want to get the current page that's being displayed (maybe page isn't the correct term). I can't find any variable that holds this. But I think it must be held somewhere, since the indicator is able to show which sub-view of the scroll view is currently being displayed.

在我的滚动视图中,我想获取正在显示的当前页面(可能 page 不是正确的术语)。我找不到任何包含这个的变量。但我认为它必须放在某个地方,因为指示器能够显示当前正在显示滚动视图的哪个子视图。

Is this hidden from us completely or is there a way for me to access it?

这是对我们完全隐藏还是有办法让我访问它?

回答by AlexVogel

There is no UIScrollViewproperty for the current page. You can calculate it with:

UIScrollView当前页面没有属性。您可以通过以下方式计算:

int page = scrollView.contentOffset.x / scrollView.frame.size.width;

If you want to round up or down to the nearest page, use:

如果要向上或向下舍入到最近的页面,请使用:

CGFloat width = scrollView.frame.size.width;
NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;

回答by Ji?í Zahálka

I pretty recommend you to use this code

我非常建议您使用此代码

int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;

but if you use this code your view doesn't need to be exactly on the page that indexOfPage gives you. It because I also recommend you to use this code only in this method

但是如果您使用此代码,您的视图不需要完全位于 indexOfPage 给您的页面上。因为我还建议您仅在此方法中使用此代码

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{}

which is called when your scrollView finishes scrolling and to have number of your page really sharp

当您的 scrollView 完成滚动并让您的页面数量非常清晰时调用

I recommend you to set your scrollView to paged enabled with this code

我建议您使用此代码将滚动视图设置为启用分页

[scrollView setPagingEnabled:YES];

So finally it should look like that way

所以最后它应该是这样的

-(void) methodWhereYouSetYourScrollView
{
   //set scrollView
   [scrollView setPagingEnabled:YES];
   scrollView.delegate = self;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;
   //your stuff with index
}

回答by Andrius Steponavi?ius

In swift I would do it in extension:

在 swift 我会在扩展中做到这一点:

extension UIScrollView {
    var currentPage:Int{
        return Int((self.contentOffset.x+(0.5*self.frame.size.width))/self.frame.width)+1
    }
}

Then just call:

然后只需调用:

scrollView.currentPage

回答by user363349

As above but as a category

同上但作为一个类别


@interface UIScrollView (CurrentPage)
-(int) currentPage;
@end
@implementation UIScrollView (CurrentPage)
-(int) currentPage{
    CGFloat pageWidth = self.frame.size.width;
    return floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
@end

回答by Hemang

Another way:

另一种方式

extension MyViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let width = scrollView.frame.width
        let page = Int(round(scrollView.contentOffset.x/width))
        print("CurrentPage:\(page)")
    }
}

回答by Max MacLeod

There's some good answers here already. However, for scenarios where content doesn't fit exactly into a page - and if like me you want to use the result for a UIPageControl, then it's essential to use the ceilfunction.

这里已经有一些很好的答案。但是,对于内容不完全适合页面的情况 - 如果像我一样想要将结果用于 a UIPageControl,那么使用该ceil函数是必不可少的。

Let's take an example where I have "four and a bit" pages of content.

让我们举一个例子,其中我有“四页”内容。

I'll base this on my current real life example. The content size width is 3140 points. Based upon my collection view frame size, page width is 728.

我将基于我当前的现实生活示例。内容大小宽度为 3140 磅。根据我的集合视图框架大小,页面宽度为 728。

So number of pages equals:

所以页数等于:

 3140 / 728 = 4.313 

My numberOfPagesis going to be five. Four of which will show in entirety, and the last of which - page five - will show that remaining 0.313of content.

numberOfPages的马上就要五岁了。其中四个将完整显示,最后一个(第 5 页)将显示剩余0.313的内容。

Now, numberOfPagesbeing five means that the page indices will be 0, 1, 2, 3, and 4.

现在,numberOfPages为 5 意味着页面索引将为 0、1、2、3 和 4。

When I swipe rightwards, paging towards the final offset, the scrollViewDidEndDeceleratingmethod gives a final X offset of 2412.

当我向右滑动,向最终偏移量分页时,该scrollViewDidEndDecelerating方法给出的最终 X 偏移量为 2412。

Applying the rounding calculation:

应用舍入计算:

2412 / 728 = 3.313 then rounded = 3

That's incorrect.Page user is viewing by offset should be:

那是不正确的。用户按偏移量查看的页面应该是:

Offset / Page User Is Viewing
0        0
728      1
1456     2
2184     3
2412     4

The correct calculation using ceil:

正确的计算使用ceil

private func pageForOffset(offset:CGFloat, pageWidth width:CGFloat) -> Int
{
    let page = Int(ceil(offset / width))
    NSLog("\(__FUNCTION__) offset \(offset) width \(width) page \(page) ")
    return page
}

回答by aoakenfo

Just divide the current offset by the page size:

只需将当前偏移量除以页面大小:

CGFloat pageNum = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);

回答by neptunes

For swift I would just use this

为了快速,我会使用这个

    let width = scrollView.frame.width
    let page = round(scrollView.contentOffset.x/width)

Pretty straightforward, it basically takes the scrollview x position, divides it by the scrollview width, then rounds it.

非常简单,它基本上采用滚动视图 x 位置,将其除以滚动视图宽度,然后将其四舍五入。

回答by pabloverd

The solution of Aoakenfo is amazing, this is another solution combining your code with the function scrollViewDidScroll and PageController

Aoakenfo的解决方案是惊人的,这是另一种解决方案你的代码的功能scrollViewDidScroll和结合的PageController

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (sender == self.scroll) {
        int pageNum = (int)(self.scrPage.contentOffset.x / self.scrPage.frame.size.width);
        self.pagecontroller.currentPage =pageNum;
    }
}

回答by Alessandro Ornano

In xCode 7.x swift 2.x you can do :

在 xCode 7.x swift 2.x 中,您可以执行以下操作:

//MARK: - ScrollView Extensions
// Get the current page number
extension UIScrollView {
    var currentPage: Int {
        return Int(round(self.contentOffset.x / self.bounds.size.width))
    }

// If you have reversed offset (start from contentSize.width to 0)
    var reverseCurrentPage: Int {
        return Int(round((contentSize.width - self.contentOffset.x) / self.bounds.size.width))-1
    }
}