How to have a UISwipeGestureRecognizer AND UIPanGestureRecognizer work on the same view

后端 未结 4 1103
春和景丽
春和景丽 2020-12-04 19:58

How would you setup the gesture recognizers so that you could have a UISwipeGestureRecognizer and a UIPanGestureRecognizer work at the same

4条回答
  •  遥遥无期
    2020-12-04 20:23

    Here is a full solution for detecting pan and swipe directions (utilizing 2cupsOfTech's swipeThreshold logic):

    public enum PanSwipeDirection: Int {
        case up, down, left, right, upSwipe, downSwipe, leftSwipe, rightSwipe
        public var isSwipe: Bool { return [.upSwipe, .downSwipe, .leftSwipe, .rightSwipe].contains(self) }
        public var isVertical: Bool { return [.up, .down, .upSwipe, .downSwipe].contains(self) }
        public var isHorizontal: Bool { return !isVertical }
    }
    
    public extension UIPanGestureRecognizer {
    
       var direction: PanSwipeDirection? {
            let SwipeThreshold: CGFloat = 1000
            let velocity = self.velocity(in: view)
            let isVertical = abs(velocity.y) > abs(velocity.x)
            switch (isVertical, velocity.x, velocity.y) {
            case (true, _, let y) where y < 0: return y < -SwipeThreshold ? .upSwipe : .up
            case (true, _, let y) where y > 0: return y > SwipeThreshold ? .downSwipe : .down
            case (false, let x, _) where x > 0: return x > SwipeThreshold ? .rightSwipe : .right
            case (false, let x, _) where x < 0: return x < -SwipeThreshold ? .leftSwipe : .left
            default: return nil
            }
        }
    
    }
    

    Usage:

    @IBAction func handlePanOrSwipe(recognizer: UIPanGestureRecognizer) {
    
        if let direction = recognizer.direction {
            if direction == .leftSwipe {
                //swiped left
            } else if direction == .up {
                //panned up
            } else if direction.isVertical && direction.isSwipe {
                //swiped vertically
            }
        }
    
    }
    

提交回复
热议问题