質問

I neet to capture all the main gestures. for example I want to be able to track when the user taps once, taps twice, taps once with one finger, taps twice with two fingers, when he pinches, when he rotates the fingers, when he drags one finger when he drags two fingers.

I know how to do all this functions the problem is that I don't want to fire two events. For example when the user pinches I don't what the drag event to fire. Also when the user drags one finger I don't want the tap function to fire.

this is what I have so far:

-(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];

}

I know I am missing a lot of implementation but I don't want to keep working on it until I am sure I am on the write path. for example maybe I should use the UITapGestureRecognizer class instead and do something like:

UITapGestureRecognizer * r = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewWasDoubleTapped:)];
[r setNumberOfTapsRequired:2];
[r setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:r];
役に立ちましたか?

解決

Yes! You should definitely use the gesture recognizers for this purpose. There are gesture recognizer for everything you have mentioned in your original post.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top