How to detect whether page is scrolled or not in ScrollView?
I have an ScrollView for display PDF Pages. When I scroll the this shows me the second Page with paging enabled in ScrollView. I set the bounce property false for Scrolling. I want to display page number that which page is being Displayed. For that I am had used following code..
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (scrollView == scrlView) {
if (lastContentOffsetX > scrollView.contentOffset.x) {
if (currentPage > 1) {
currentPage--;
self.title = [NSString stringWithFormat:@"%d/%d", currentPage, totalPages];
}
}
else if (lastContentOffsetX < scrollView.contentOffset.x) {
if (currentPage < totalPages) {
currentPage++;
self.title = [NSString stringWithFormat:@"%d/%d", currentPage, totalPages];
}
}
lastContentOffsetX = scrollView.contentOffset.x;
}
}
I don't have any problem if every time I drag and the page changes. Some time I drag a very little time and the page doesn't change and remain the same. My page number get Increased/开发者_高级运维decreased.
Can Any body tell me if I can get any event when ScrollView Stops Scrolling I can get the current offset and complete my task.
You change your page number every time user drags the scrollview - simply using the fact that the dragging occurred you assume that the page changed - that's not always so. In your code just calculate your current page from contents offset and page width. This way you can calculate current page number (0-based):
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (scrollView == scrlView) {
CGFloat xOffset = scrollView.contentOffset.x;
int currentPage = floor(xOffset / scrollView.bounds.size.width);
self.title = [NSString stringWithFormat:@"%d/%d", currentPage, totalPages];
}
}
Also you may be using incorrect delegate method - I'd use scrollViewDidScroll:
method instead
P.S. to show the number for a page that currently occupies more than half of the screen change the code to:
int currentPage = floor(xOffset / scrollView.bounds.size.width + 0.5);
You can use the delegate method of UIScrollView.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Please refer the documentation UIScrollViewDelegate Protocol Reference
Try
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
and/or
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
精彩评论