开发者

How to page-scroll on a uiscrollview?

If I've got a uiscrollview which is five pages wide, what code would tell me what page I am on, when I scroll to a new page? Also, what code 开发者_如何学Pythonwould scroll to a specific page?

Thanks!


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int currentPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = currentPage;
}

I have the scrollview frame width as the page width. If you are using something else as page width, make respective changes to get this to work

for scrolling to current page - if I understand it right, you have an option to enter page number and choose to scroll to that page:

Just the code above, do it reverse order, and solve for contentOffset.X

once you have the x value, create a frame with that x in it, and scrollToRect: animated: will do the work.


Check out the PageControl example from Apple:

http://developer.apple.com/iphone/library/samplecode/PageControl/Listings/ReadMe_txt.html


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scroller_x = [scrollView contentOffset].x;
CGFloat scroller_y = [scrollView contentOffset].y;
NSLog(@"%f, %f",scroller_x,scroller_y);
}

Once this delegate method fires (on scroll) check it against each views frame.origin.x and frame.origin.y The view's frame that matches will be the view that you scrolled to.

[scrollView setContentOffset:<#(CGPoint)#>];

use this method to scroll to a specific view.frame.origin. So suppose you have an action to go to a specific view, you can use [scrollView setContentOffset:view.frame.origin] to get there.

(assumed paging is enabled)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜