How to get UIScrollView vertical direction in Swift?

前端 未结 12 1012
悲&欢浪女
悲&欢浪女 2020-12-01 01:51

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

12条回答
  •  醉话见心
    2020-12-01 02:32

    you could do something like this:

    fileprivate var lastContentOffset: CGPoint = .zero
    
    func checkScrollDirection(_ scrollView: UIScrollView) -> UIScrollViewDirection {
        return lastContentOffset.y > scrollView.contentOffset.y ? .up : .down
    }
    

    and with scrollViewDelegate:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        switch checkScrollDirection(scrollView) {
        case .up:
            // move up
        case .down:
            // move down
        default:
            break
        }
    
        lastContentOffset = scrollView.contentOffset
    }
    

提交回复
热议问题