How to get UIScrollView vertical direction in Swift?

前端 未结 12 987
悲&欢浪女
悲&欢浪女 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:42

    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 the scrollView.contentOffset.y range to be greater than 0 or less than scrollView.contentSize.height - scrollView.frame.height, no changes are made when bouncing back.

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
            // move up
        } else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
            // move down
        }
    
        // update the new position acquired
        lastContentOffset = scrollView.contentOffset.y
    }
    

提交回复
热议问题