判断ScrollView滚动方向

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滚动方向)