如何制作iOS自定义手势识别器?

时间:2014-09-12 03:03:53

标签: ios objective-c uigesturerecognizer gesture

我正在尝试在我的应用中实现一项功能,您可以用手指画半圈,以便做一些事情。我如何制作一个自定义手势识别器来跟踪用户是否用手指在屏幕上制作半圈?

1 个答案:

答案 0 :(得分:1)

@implementation ViewController
{
    NSInteger level;
    CGPoint   oldPoint;
    CGPoint   newPoint;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"start");
    level = 0;
    UITouch *touch = [touches anyObject];
    newPoint = [touch locationInView:self.view];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    oldPoint = newPoint;
    newPoint = [touch locationInView:self.view];

    if (newPoint.y > oldPoint.y && newPoint.x < oldPoint.x && level==0) {
        level += 1;
    }

    if (newPoint.y > oldPoint.y && newPoint.x > oldPoint.x && level==1) {
        level += 1;
    }
    if (newPoint.y < oldPoint.y && newPoint.x > oldPoint.x && level==2) {
        level += 1;
    }
    if (newPoint.y < oldPoint.y && newPoint.x < oldPoint.x && level==3) {
        level += 1;
    }

    if (4 == level) {
        level = 0;
        NSLog(@"successs");
    }
}