适用于iOS的特殊手势

时间:2011-07-25 21:44:19

标签: iphone ios gesture swipe

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

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

我在考虑对其中一个UIGesture类进行子类化,但不确定如何检测我需要的内容。

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

1 个答案:

答案 0 :(得分:3)

你真的不能认出一个“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;
    }

}

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