4

I want to know index of current scrolled page in UIScrollView. I have searched through many sites by none helps. Any suggestions are welcome.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1564754
  • 41
  • 1
  • 1
  • 3
  • similar question it's work for me [link] http://stackoverflow.com/questions/4132993/getting-the-current-page – sash Jul 21 '15 at 06:28
  • note that the answer here may be as simple as using views.index#of ! **let index = v.superview?.subviews.index(of: v)** works perfectly in a UIScrollView – Fattie Apr 20 '17 at 14:21
  • the only wholly correct answer https://stackoverflow.com/a/76058339/294884 – Fattie Apr 19 '23 at 19:57

4 Answers4

6

Try this

    //Horizontal
    NSInteger pagenumber = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //Vertical
    NSInteger pagenumber = scrollView.contentOffset.y / scrollView.bounds.size.height;
Kyle Howells
  • 3,008
  • 1
  • 25
  • 35
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
5

For those needing code (swift) , the UIScrollViewDelegate provides the method, this uses the horizontal example from above

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    print("END Scrolling \(scrollView.contentOffset.x / scrollView.bounds.size.width)")
    index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
}
Mindeater
  • 322
  • 3
  • 9
  • wrong, unfortunately. You have to correctly round and encapsulate, or it will (a) often be wrong (b) often crash – Fattie Apr 19 '23 at 19:54
0

Use the contentOffset property, which returns a CGSize that is the offset of the content in the UIScrollView.

craigmj
  • 4,827
  • 2
  • 18
  • 22
-1

with the help of some mathematical calculation you can get index number of page I am sending code for reference only . I have done.

-(void)KinarafindingSelectedCategory:(UIScrollView*)scrollView{
    for (UIView *v in scrollView.subviews)
    {
        if ([v isKindOfClass:[UIButton class]])
        {
            UIButton *btn=(UIButton*)v;

            float x1=scrollView.contentOffset.x+(([UIScreen mainScreen].bounds.size.width/2)-(btn.frame.size.width/2));
            float x2=scrollView.contentOffset.x+(([UIScreen mainScreen].bounds.size.width/2)+(btn.frame.size.width/2));
            float BtnMidPoint=btn.frame.origin.x+(btn.frame.size.width/2);
            if(BtnMidPoint >= x1 && BtnMidPoint <= x2)
            {
                if(scrollView==KinaraCategory)
                {
                    KinaraSelectedCategoryName=btn.titleLabel.text;
                    KinaraSelectedCategoryID=btn.tag;
                    NSLog(@"Selected Category Tag : %d",KinaraSelectedCategoryID);
                    NSLog(@"Selected Category Name : %@",KinaraSelectedCategoryName);
                }
                else if(scrollView==KinaraSubCategory)
                {
                    KinaraSelectedSubCategoryID=btn.tag;
                    KinaraSelectedSubCategoryName=btn.titleLabel.text;

                    NSLog(@"Selected SubCategory Tag : %d",KinaraSelectedSubCategoryID);
                    NSLog(@"Selected SubCategory Name : %@",KinaraSelectedSubCategoryName);
                }
                break;
            }
        }
    }

}