Demo code
https://github.com/Sylvia-YiyinShen/ScollViewLab
When we are trying to add pulldown refreshing functionality or sticky header, we will realise the scroll view delegate function might be the right place to look at. But what is the exactly right function to override?
First let us put print message inside each function and print 'decelerate' value in scrollViewDidEndDragging
Drag up and make the tableView stop, you will see the print as below:
scrollViewWillBeginDragging
scrollViewDidScroll
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: false
scrollViewDidScroll
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: false
Drag down, keep the tableView scrolling and decelerating you will see the print like this:
scrollViewWillBeginDragging
scrollViewDidScroll
scrollViewDidScroll
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: true
scrollViewWillBeginDecelerating
scrollViewDidScroll
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDecelerating
scrollViewDidScroll
scrollViewDidScroll
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: true
scrollViewWillBeginDecelerating
scrollViewDidScroll
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDecelerating
What about drag up?
scrollViewWillBeginDragging
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: true
scrollViewWillBeginDecelerating
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDecelerating
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDragging willDecelerate: true
scrollViewWillBeginDecelerating
scrollViewDidScroll
...
scrollViewDidScroll
scrollViewDidEndDecelerating
Based on the above observation, we know
1. As long as the contentOffset value of the tableView/scrollView changes, scrollViewDidScroll will always be triggered
2. A drag gesture will definitely trigger scrollViewWillBeginDragging and scrollViewDidEndDragging, but not necessarily scrollViewWillBeginDecelerating and scrollViewDidEndDecelerating. It depends on when drag gesture finished(finger leaves the screen) if the scrollView would still scroll - the 'decelerate' value in scrollViewDidEndDragging
3. Pulling down will always trigger the above delegate functions, since tableView would bounce to the top
No comments:
Post a Comment