代码之家  ›  专栏  ›  技术社区  ›  Parth Bhatt

如何在iOS中检测刷卡手势?

  •  34
  • Parth Bhatt  · 技术社区  · 14 年前

    在我的iPhone应用程序中,我需要识别用户在视图上的刷卡手势。

    我希望刷卡手势被识别,并在刷卡时执行功能。

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

    需要做什么?

    我怎么能认出它?

    3 回复  |  直到 6 年前
        1
  •  41
  •   jer    14 年前

    使用 UISwipeGestureRecognizer . 其实没什么好说的,手势识别器很简单。有 WWDC10 videos 甚至在这个问题上。第120和121课时。:)

        2
  •  44
  •   Guntis Treulands    9 年前

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

    UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething)];
    
    [mSwipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
    
    [[self view] addGestureRecognizer:mSwipeUpRecognizer];
    

    在.h文件中添加:

    <UIGestureRecognizerDelegate>
    
        3
  •  0
  •   King-Wizard    9 年前

    下面的链接将您重定向到一个视频教程,该教程解释了如何在 目标C :

    UISwipeGestureRecognizer Tutorial (Detecting swipes on the iPhone)

    下面的代码示例,在 斯威夫特 :

    你需要一个 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
            }
        }
    }