如何在iPhone SDK中检测滑动手势?

时间:2022-09-06 21:09:24

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?

我怎么认出来的?

Any directions or tutorials will be really appreciated.

任何指示或教程将非常感谢。

4 个解决方案

#1


41  

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

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

#2


45  

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>

#3


6  

theres apple documentation and sample for swipe recognition refer below link;

用于刷卡识别的苹果文档和示例请参考以下链接;

http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

#4


-1  

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 UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property 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
        }
    }
}

#1


41  

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

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

#2


45  

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>

#3


6  

theres apple documentation and sample for swipe recognition refer below link;

用于刷卡识别的苹果文档和示例请参考以下链接;

http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

http://developer.apple.com/library/ios/#samplecode/SimpleGestureRecognizers/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009460

#4


-1  

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 UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property 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
        }
    }
}