Is there a way to detect or get a notification when user changes the page in a paging-enabled UIScrollView?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
Use this to detect which page is currently being shown and perform some action on page change:
If it's acceptable for you to only react to the page change once the scrolling has completely stopped, then it would be best to do the above inside the
scrollViewDidEndDecelerating:
delegate method instead of thescrollViewDidScroll:
method.Swift 4
I found the best way to do this is by using
scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)
. It lets you predict if paging will occur as soon as you lift your finger off the screen. This example is for paging horizontally.Remember to the assign the
scrollView.delegate
to the object that adoptsUIScrollViewDelegate
and implements this method.How about combining two methods of UIScrollViewDelegate?
In
scrollViewDidEndDragging(_:willDecelerate:)
, if it stops right away, we do the page calculation; if it is decelerating, we let it go and it will be caught byscrollViewDidEndDecelerating(_:)
.The code is tested with XCode version 7.1.1, Swift version 2.1
First result on google for page detection so I had to answer this with a better solution in my opinion. (even if this question was asked 2 and a half years ago.)
I'd prefer not to call
scrollViewDidScroll
just to track the page number. Thats an overkill for something simple as that. UsingscrollViewDidEndDecelerating
does work and stops on a page change BUT (and it's a big but) if the user will swipe on the screen twice a bit faster than normalscrollViewDidEndDecelerating
will be called only once. You can easily go from page #1 to page #3 without processing page #2.This solved it completely for me:
That way the user can only swipe one page at a time without the risk described above.
In paging enabled scroll view you can use scrollViewDidEndDecelerating to know when the view is settled on a page (might be the same page).
scrollViewDidScroll
gets called on every movement. And in context of paging enabled view can be used to find when it is scrolled enough to move to next page (if dragging is stopped at that point).Implement the delegate of UIScrollView. This method is what you are looking for.