简体   繁体   English

在分页scrollView中跟踪页面

[英]Track pages in paging scrollView

On my iOS app (iOS 11, Swift 4), there is a paging scroll view. 在我的iOS应用(iOS 11,Swift 4)上,有一个分页滚动视图。 To track what page the user is currently is looking at, I am trying to use the scrollViewWillBeginDragging , scrollViewDidEndDragging and scrollViewDidEndDecelerating methods. 为了跟踪用户当前正在查看的页面,我尝试使用scrollViewWillBeginDraggingscrollViewDidEndDraggingscrollViewDidEndDecelerating方法。

However, these seem to not be enough. 但是,这些似乎还不够。 I'll use an example. 我将举一个例子。 Let's say the scrollView has 2 pages, and the user is looking at the one on the most left (index 0). 假设scrollView有2页,而用户正在查看最左侧的那一页(索引0)。

  • If the user, on single gesture, swipes right, the everything works great (beginDragging is called, then end dragging is called and finally endDecelerating is called) 如果用户以单一手势向右滑动,则一切正常(调用beginDragging,然后调用结束拖动,最后调用endDecelerating)

  • If the user tries to swipe left (to where there is no more pages), when the user releases the touch the UI 'snaps back', as it should. 如果用户尝试向左滑动(到没有更多页面的位置),则当用户释放触摸时,UI会“向后快照”。 The problem here, is that although didEndDragging is called with its willDecelerate parameter set to true, didEndDecelerating is never called. 这里的问题是,虽然didEndDragging被称为其willDecelerate设置为true的参数, didEndDecelerating不会被调用。 And this makes me loose track of the page currently being viewed. 这使我无法跟踪当前正在查看的页面。

I need to know when the users starts to scroll, and when the scrolling stops. 我需要知道用户何时开始滚动以及何时停止滚动。 Are there any better strategies to achieve this? 有没有更好的策略来实现这一目标?

Thanks! 谢谢!

You could use the scrollViewDidScroll delegate method, and in there based on the contentOffset you could calculate which page is being shown. 您可以使用scrollViewDidScroll委托方法,并根据contentOffset在其中计算要显示的页面。 Also UIScrollView has isDragging, isDecelerating, etc properties which could help you. UIScrollView还具有isDragging,isDecelerating等属性,可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM