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 d
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.
enum WMScrollDirection {
case Up, Down, None
}
class WMScrollView: UIScrollView {
var lastDirection: WMScrollDirection = .None {
didSet {
if oldValue != lastDirection {
// direction has changed, call your func here
}
}
}
override var contentOffset: CGPoint {
willSet {
if contentOffset.y > newValue.y {
lastDirection = .Down
}
else {
lastDirection = .Up
}
}
}
}
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