如何在 iOS 中检测滑动手势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:09:19  来源:igfitidea点击:

How to detect Swipe Gesture in iOS?

iosuigesturerecognizerswipegesture-recognition

提问by Parth Bhatt

In my iPhone app, I require to recognize the swipe gesture made by the user on the view.

在我的 iPhone 应用程序中,我需要识别用户在视图上所做的滑动手势。

I want the swipe gestures to be recognized and perform a function on swipe.

我希望能够识别滑动手势并在滑动时执行功能。

I need that the view should horizontally slide and show another view as a user makes a swipe gesture.

我需要视图应该水平滑动并在用户做出滑动手势时显示另一个视图。

What needs to be done?

需要做什么?

How do I recognize it?

我如何识别它?

回答by Guntis Treulands

If You know how it works, but still need a quick example, here it is! (it will become handy at least for me, when I will need copy-paste example, without trying remembering it)

如果你知道它是如何工作的,但仍然需要一个快速的例子,它就在这里!(它至少对我来说会很方便,当我需要复制粘贴示例时,无需尝试记住它)

UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];

[mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];

[[self view] addGestureRecognizer:mSwipeUpRecognizer];

and in .h file add:

并在 .h 文件中添加:

<UIGestureRecognizerDelegate>

回答by jer

Use the UISwipeGestureRecognizer. Not much else to say really, gesture recognizers are easy. There are WWDC10 videoson the subject even. Sessions 120 and 121. :)

使用UISwipeGestureRecognizer. 真的没什么可说的,手势识别器很容易。甚至还有关于这个主题的WWDC10 视频。第 120 和 121 节。:)

回答by King-Wizard

The following link below redirects you to a video tutorial which explains you how to detect swipes on the iPhone in Objective-C:

以下链接将您重定向到一个视频教程,该视频教程向您解释了如何在Objective-C 中检测 iPhone 上的滑动:

UISwipeGestureRecognizer Tutorial (Detecting swipes on the iPhone)

UISwipeGestureRecognizer 教程(检测 iPhone 上的滑动)

Code sample below, to achieve that in Swift:

下面的代码示例,以在Swift 中实现:

You need to have one UISwipeGestureRecognizerfor each direction. It's a little weird because the UISwipeGestureRecognizer.directionproperty is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:

UISwipeGestureRecognizer每个方向都需要一个。这有点奇怪,因为该UISwipeGestureRecognizer.direction属性是一个选项样式的位掩码,但每个识别器只能处理一个方向。如果需要,您可以将它们全部发送到同一个处理程序,并在那里进行分类,或者将它们发送到不同的处理程序。这是一种实现:

override func viewDidLoad() {
    super.viewDidLoad()

    var swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeRight.direction = UISwipeGestureRecognizerDirection.Right
    self.view.addGestureRecognizer(swipeRight)

    var swipeDown = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down
    self.view.addGestureRecognizer(swipeDown)
}

func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.Right:
            println("Swiped right")
        case UISwipeGestureRecognizerDirection.Down:
            println("Swiped down")
        default:
            break
        }
    }
}