Getting the current page

2019-01-29 19:19发布

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.

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

13条回答
SAY GOODBYE
2楼-- · 2019-01-29 19:42

I extracted this out of a couple of our apps...

- (NSInteger)currentPage
{
    if (0 == self.frame.size.width) {
        NSLog(@"frame width == 0!");
        return 0;
    }
    if (! self.currentPageValid) {
        _currentPage = round(self.contentOffset.x / self.frame.size.width);
        self.currentPageValid = YES;
    }
    return _currentPage;
}

Full source here

查看更多
太酷不给撩
3楼-- · 2019-01-29 19:43

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.

查看更多
贼婆χ
4楼-- · 2019-01-29 19:44

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

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;
查看更多
【Aperson】
5楼-- · 2019-01-29 19:45

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

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

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

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
}
查看更多
贪生不怕死
6楼-- · 2019-01-29 19:46

The solution of Aoakenfo is amazing, this is another solution combining your code with the function scrollViewDidScroll and 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;
    }
}
查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-29 19:48

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
查看更多
登录 后发表回答