how detect swipe gesture direction?

前端 未结 4 2060
有刺的猬
有刺的猬 2020-12-02 21:06

i need to detect direction of my swipe gesture and i\'ve got problem with it. gesture is working, but i don\'t know how to detect direction. ...

swipeGe         


        
4条回答
  •  攒了一身酷
    2020-12-02 21:41

    Swipe Gesture direction detect with Swift 5

        override func viewDidLoad() {
        super.viewDidLoad()
    
             let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
             let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
    
             leftSwipe.direction = .left
             rightSwipe.direction = .right
    
             view.addGestureRecognizer(leftSwipe)
             view.addGestureRecognizer(rightSwipe)
        }
    
        @objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
    
            if (sender.direction == .left) {
                    print("Swipe Left")
            }
    
            if (sender.direction == .right) {
                print("Swipe Right")
            }
        }
    

提交回复
热议问题