捕获同一视图上的所有主要手势

时间:2012-05-02 18:49:03

标签: ios objective-c touches

我需要捕捉所有主要姿势。例如,我希望能够跟踪用户点击一次,点击两次,用一根手指点击一次,用两根手指点击两次,当他捏,当他旋转手指时,当他拖动一根手指时,他拖动两根手指。

我知道如何做所有这些功能的问题是我不想发射两个事件。例如,当用户捏时我不会触发什么阻力事件。此外,当用户拖动一根手指时,我不希望点击功能触发。

这是我到目前为止所做的:

-(void)singleTap:(NSIndexPath *)event
{

}

-(void)doubleTapMethod:(id)event
{
    if(enableTap==NO){
        enableMove=YES;
        return;
    }

     NSLog(@"double tap with number of fingers = %i",TouchesCount); 
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    enableTap=YES;
    enableMove=NO;


    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    TapCount = touch.tapCount;


    X=location.x;
    Y=location.y;

    NSData *data = [ @"SSSSS" dataUsingEncoding:NSUTF8StringEncoding];

    [udpSocket sendData:data toHost:IP port:Port withTimeout:-1 tag:1];


    switch (TapCount) {
        case 1:
            [self performSelector:@selector(singleTap:) withObject:nil afterDelay: delay];
            break;
        case 2:
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap:) object:nil];
            [self performSelector:@selector(doubleTapMethod:) withObject:nil afterDelay:delay];
            break;
        case 3:
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doubleTapMethod:) object:nil];
            //[self performSelector:@selector(tripleTapMethod) withObject:nil afterDelay:delay];
            break;
        case 4:
            //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tripleTapMethod) object:nil];
            //[self quadrupleTap];
            break;
        default:
            break;
    }

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{


    enableTap=NO;

    if(enableMove==NO)
        return;

    NSLog(@"touches moved %i",TapCount);
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];    

    NSData *data = [ [NSString stringWithFormat:@"%i,%i", ((int)location.x)-X,((int)location.y)-Y] dataUsingEncoding:NSUTF8StringEncoding];

    [udpSocket sendData:data toHost:IP port:Port withTimeout:-1 tag:1];


}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    TouchesCount = touches.count;
    return;//here


    NSData *data = [ @"EEEEEE" dataUsingEncoding:NSUTF8StringEncoding];
    [udpSocket sendData:data toHost:IP port:Port withTimeout:-1 tag:1];

}

我知道我缺少很多实现,但我不想继续努力,直到我确定我在写路径上。例如,也许我应该使用UITapGestureRecognizer类来做类似的事情:

UITapGestureRecognizer * r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWasDoubleTapped:)];
[r setNumberOfTapsRequired:2];
[r setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:r];

1 个答案:

答案 0 :(得分:1)

是的!你应该为此目的使用手势识别器。您在原始帖子中提到的所有内容都有手势识别器。