How can I get the scroll/swipe direction for up/down in a VC?
I want to add a UIScrollView or something else in my VC that can see if the user swipes/scrolls up or down and then hide/show a UIView
depending if it was an up/down gesture.
How can I get the scroll/swipe direction for up/down in a VC?
I want to add a UIScrollView or something else in my VC that can see if the user swipes/scrolls up or down and then hide/show a UIView
depending if it was an up/down gesture.
Swift 2-4 with UISwipeGestureRecognizer
Another option, is use to use the
UISwipeGestureRecognizer
that will recognize the swipe to requested direction (That will work on all views and not only onUIScrollView
I made protocol to reuse Scroll Directions.
Declare these
enum
andprotocol
s.Usage From
ViewController
If you want to use specific direction, just update specific
contentOffset
that you want.For Swift I think the simplest and most powerful is to do like below. It allows you to track when direction changed and react only once when it changed. Plus you can always access the
.lastDirection
scrolled property if you need to consult that in your code at any other stage.The above assumes you are only tracking up/down scrolling. It is customizable via the enum. You could add/change to
.left
and.right
to track any direction.I hope this helps someone.
Cheers
you could do something like this:
and with scrollViewDelegate:
I used Victor's answer with a minor improvement. When scrolling past the end or beginning of the scroll, and then getting the bounce back effect. I have added the constraint by calculating
scrollView.contentSize.height - scrollView.frame.height
and then limiting thescrollView.contentOffset.y
range to be greater than 0 or less thanscrollView.contentSize.height - scrollView.frame.height
, no changes are made when bouncing back.