How to detect a flick gesture using UISwipeGestureRecognizer?

我与影子孤独终老i 提交于 2019-12-11 08:35:12

问题


I heard that UISwipeGestureRecognizer can be used to distinguish between a slow swipe and a fast flick. But I can't find any API to tell the difference.


回答1:


Do you specifically need to use UISwipeGestureRecognizer, or can you use UIPanGestureRecognizer instead? UIPanGestureRecognizer gives you precise movement data whereas UISwipeGestureRecognizer is more basic and just detects whether or not a swipe happened (and in which direction).

UIPanGestureRecognizer has a -velocityInView: method which returns a CGPoint, expressing points per second, vertically and horizontally.




回答2:


You should use Pan Gesture and find out velocity for better accuracy.

Here is code snip for PanGesture in Swift 5

//Add Pan Gesture on target view in viewDidLoad
        let panGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.panGestureDetected))
        view.addGestureRecognizer(panGesture)

@objc func panGestureDetected()
    {
        print("Pan Gesture detected!!")
    }


来源:https://stackoverflow.com/questions/21487750/how-to-detect-a-flick-gesture-using-uiswipegesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!