【问题标题】:Special gestures for iOSiOS 的特殊手势
【发布时间】:2011-07-26 06:24:58
【问题描述】:

我想为我的应用程序添加特殊手势。

例如,如果用户在屏幕上滑动 X,我想将其视为删除,或者如果用户将滑动 V - 我想将其视为确认。

我正在考虑子类化 UIGesture 类之一,但不确定如何检测我需要的内容。

更新:我找到了一个复选标记手势示例 (http://conceitedcode.com/2010/09/custom-gesture-recognizers/),但不知道如何实现 X。

【问题讨论】:

  • iOS 可能还不支持像绘图这样的随机手势。大多数可用的手势都是定义明确的滑动、点击和转动。 iOS 5 似乎确实有类似这样的功能,称为辅助触控,这可能表明以后能够重载自定义绘图风格的手势。

标签: iphone ios gesture swipe


【解决方案1】:

您无法真正识别“X”,那将是 2 个手势。你必须做某种手势保护程序来查看前一个是否是对角线,如果这个是一个......以及各种疯狂。你可以做一些事情,比如对角向下,笔直向上,然后在另一个方向对角向下。我把代码留给你:P

但是,您要做的是 UIGestureRecognizer 的子类。 Here is some documentation on it。您需要实现以下方法:

- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

这是用于识别“V”手势的代码。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    if ([self state] == UIGestureRecognizerStateFailed) 
        return;

    CGPoint curr = [[touches anyObject] locationInView:self.view];
    CGPoint prev = [[touches anyObject] previousLocationInView:self.view];

    if (!strokeUp) {
        // upstroke has increasing x value but decreasing y value
        if (curr.x >= prev.x && curr.y <= prev.y) {
            strokeUp = YES;
        } else {
            [self state] = UIGestureRecognizerStateFailed;
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    if (([self state] == UIGestureRecognizerStatePossible) && strokeUp) {
        [self state] = UIGestureRecognizerStateRecognized;
    }

}

这应该会让你指向正确的方向,祝你好运。

【讨论】:

  • 你实际上给了我一个关于如何解决它的想法:实现一个“对角线”手势,然后检查其中的两个 - 彼此非常接近(例如,不到 1-2 秒) .然后我会根据我的需要采取行动。
猜你喜欢
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多