Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: how to get index of current ?UIscrollView page

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

like image 871
user1564754 Avatar asked Dec 12 '22 21:12

user1564754


2 Answers

Try this

    //Horizontal
    NSInteger pagenumber = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //Vertical
    NSInteger pagenumber = scrollView.contentOffset.y / scrollView.bounds.size.height;
like image 92
Lithu T.V Avatar answered Jan 08 '23 22:01

Lithu T.V


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)
}
like image 44
Mindeater Avatar answered Jan 08 '23 21:01

Mindeater