Detect direction of UIScrollView scroll in scrollViewWillBeginDragging

后端 未结 9 1818
心在旅途
心在旅途 2020-12-17 09:30

I did google enough, & I did check posts like these ( Finding the direction of scrolling in a UIScrollView? ) in stackoverflow before posting this. I have a dynamic numb

相关标签:
9条回答
  • 2020-12-17 10:07

    Take a look at scrollViewWillEndDragging:withVelocity:targetContentOffset:. You can use that method do do any checking and see if it is going where it should not and then in the method you can set a new targetContentOffset.

    Per the documentation:

    This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.

    0 讨论(0)
  • 2020-12-17 10:09

    This is what I used and it works nicely at least on iOS 6.0:

    - (void)scrollViewDidScroll:(UIScrollView*)scrollView
    {
       CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView];
    
       // Detect direction by accessing x or y of translation
    }
    

    Saves you the instance variable for lastContentOffset ...

    0 讨论(0)
  • 2020-12-17 10:11

    Thank you, Kris. This is what worked for me, finally:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        // Detect the scroll direction
        if (lastContentOffset < (int)scrollView.contentOffset.x) {
        ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题