ScrollView中如何检测页面是否滚动?
我有一个用于显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次用户拖动滚动视图时,您都会更改页码 - 只需使用拖动发生的事实,您就假设页面已更改 - 情况并非总是如此。在您的代码中,只需根据内容偏移量和页面宽度计算当前页面。这样您就可以计算当前页码(从 0 开始):
您也可能使用了不正确的委托方法 - 我会使用
scrollViewDidScroll:
方法而不是要显示当前占据一半以上屏幕的页面的编号,请将代码更改为:
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):
Also you may be using incorrect delegate method - I'd use
scrollViewDidScroll:
method insteadP.S. to show the number for a page that currently occupies more than half of the screen change the code to:
您可以使用 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
尝试
和/或
Try
and/or