美文网首页
判断ScrollView滚动方向

判断ScrollView滚动方向

作者: Sober_DeTong | 来源:发表于2021-11-19 18:34 被阅读0次
    enum LiveListScrollDirection: Int {
        case none
        case crazy
        case left
        case right
        case up
        case down
        case horizontal
        case vertical
    }
        func determineScrollDirection(scrollView: UIScrollView) -> LiveListScrollDirection {
            var scrollDirection: LiveListScrollDirection
            
            if self.initialContentOffset.x != scrollView.contentOffset.x && self.initialContentOffset.y != scrollView.contentOffset.y {
                scrollDirection = .crazy
            } else {
                if self.initialContentOffset.x > scrollView.contentOffset.x {
                    scrollDirection = .left
                } else if self.initialContentOffset.x < scrollView.contentOffset.x {
                    scrollDirection = .right
                } else if self.initialContentOffset.y > scrollView.contentOffset.y {
                    scrollDirection = .up
                } else if self.initialContentOffset.y < scrollView.contentOffset.y {
                    scrollDirection = .down
                } else {
                    scrollDirection = .none
                }
            }
            return scrollDirection
        }
        
        func determineScrollDirectionAxis(scrollView: UIScrollView) -> LiveListScrollDirection {
            let scrollDirection: LiveListScrollDirection = self.determineScrollDirection(scrollView: scrollView)
        
            switch scrollDirection {
            case .left,.right:
                return .horizontal
            case .up,.down:
                return .vertical
            default:
                return .none
            }
    
    

    参考地址

    相关文章

      网友评论

          本文标题:判断ScrollView滚动方向

          本文链接:https://www.haomeiwen.com/subject/tiretrtx.html