How to get UIScrollView vertical direction in Swift?

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

    If you use an UIScrollView then you can take benefit from the scrollViewDidScroll: function. You need to save the last position (the contentOffset) it have and the update it like in the following way:

    // variable to save the last position visited, default to zero
    private var lastContentOffset: CGFloat = 0
    
    func scrollViewDidScroll(scrollView: UIScrollView!) {
        if (self.lastContentOffset > scrollView.contentOffset.y) {
            // move up
        }
        else if (self.lastContentOffset < scrollView.contentOffset.y) { 
           // move down
        }
    
        // update the new position acquired
        self.lastContentOffset = scrollView.contentOffset.y
    }
    

    There are other ways of do it of course this is one to them.

    I hope this help you.

提交回复
热议问题