ScrollView中如何检测页面是否滚动?

发布于 2024-10-24 00:52:46 字数 932 浏览 3 评论 0原文

我有一个用于显示 PDF 页面的 ScrollView。当我滚动时,这会显示在 ScrollView 中启用分页的第二页。我将滚动的弹跳属性设置为 false。我想显示正在显示的页面的页码。为此,我使用了以下代码。

- (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;
}
}

如果每次拖动并且页面发生变化,我不会有任何问题。有时我拖动了很短的时间,页面没有改变并保持不变。我的页码增加/减少。

任何人都可以告诉我当 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2024-10-31 00:52:46

每次用户拖动滚动视图时,您都会更改页码 - 只需使用拖动发生的事实,您就假设页面已更改 - 情况并非总是如此。在您的代码中,只需根据内容偏移量和页面宽度计算当前页面。这样您就可以计算当前页码(从 0 开始):

- (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];
   }
}

您也可能使用了不正确的委托方法 - 我会使用 scrollViewDidScroll: 方法而

不是要显示当前占据一半以上屏幕的页面的编号,请将代码更改为:

int currentPage = floor(xOffset / scrollView.bounds.size.width + 0.5);

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);
情深如许 2024-10-31 00:52:46

您可以使用 UIScrollView 的委托方法。

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

请参阅文档 UIScrollViewDelegate协议参考

You can use the delegate method of UIScrollView.

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

Please refer the documentation UIScrollViewDelegate Protocol Reference

绮筵 2024-10-31 00:52:46

尝试

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

和/或

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

Try

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

and/or

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文