Ivan Gersonskiy
Ivan Gersonskiy

Reputation: 17

Detecting 2 touches in same time

everyone! I need to detect 2 touches on different image views in same time. So I need to start timer when user touch both image views in same time. And stop timer when touches is end. The image views are moving on screen. There are no problems when I use one image view. Have you any ideas?

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

    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
    for (UITouch *touch2 in allTouches)

    {
        CGPoint location = [touch locationInView:touch.view];
        CGPoint location2 = [touch2 locationInView:touch2.view];

        if ([touchArea2.layer.presentationLayer hitTest:location2]) {
             touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
        }
        if ([touchArea.layer.presentationLayer hitTest:location]) {
           touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];

            timerTouch = 10;
            touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
        } else if (![touchArea.layer.presentationLayer hitTest:location]){
            [touchTimer invalidate];
            touchTimer = nil;
        } }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
        for (UITouch *touch2 in allTouches)   {


            CGPoint location = [touch locationInView:touch.view];
            CGPoint location2 = [touch2 locationInView:touch2.view];


            if (![touchArea.layer.presentationLayer hitTest:location]){
                touchArea2.image = [UIImage imageNamed:@"TouchArea"];
                touchArea.image = [UIImage imageNamed:@"TouchArea"];
                [touchTimer invalidate];
                touchTimer = nil;
            }
        }

}


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

    touchArea.image = [UIImage imageNamed:@"TouchArea"];
    [touchTimer invalidate];
}

Thanks for any help))

Upvotes: 0

Views: 316

Answers (4)

signal
signal

Reputation: 225

 UIView * view = [[UIView alloc] init];
    UITapGestureRecognizer * tap = nil;
    tap.numberOfTouchesRequired = 2;
    tap.delegate = self;
    [view addGestureRecognizer:tap];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

then you recognizer the location of two touches,and decide what you do ;

Upvotes: 0

Ivan Gersonskiy
Ivan Gersonskiy

Reputation: 17

I thought about this problem a lot and I find the way how to do that. Here is my code:

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

    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
        for (UITouch *touch2 in allTouches)



    {
        CGPoint location = [touch locationInView:touch.view];
        CGPoint location2 = [touch2 locationInView:touch2.view];


        if ([touchArea2.layer.presentationLayer hitTest:location]) {
            touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];

        }

        if ([touchArea.layer.presentationLayer hitTest:location]) {
           touchArea.image = [UIImage imageNamed:@"TouchAreaTouched"];
            if ([touchArea2.layer.presentationLayer hitTest:location2]) {
                touchArea2.image = [UIImage imageNamed:@"TouchAreaTouched"];
                            timerTouch = 10;
            touchTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
            } }}
    }


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



          {
            CGPoint location = [touch locationInView:touch.view];
              if (![touchArea.layer.presentationLayer hitTest:location]&&![touchArea2.layer.presentationLayer hitTest:location]) {
                  touchArea.image = [UIImage imageNamed:@"TouchArea"];
                  touchArea2.image = [UIImage imageNamed:@"TouchArea"];
                  [touchTimer invalidate];
              }

                 }
}


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

    touchArea.image = [UIImage imageNamed:@"TouchArea"];
    touchArea2.image = [UIImage imageNamed:@"TouchArea"];

    [touchTimer invalidate];
}

Upvotes: 0

iTukker
iTukker

Reputation: 2083

You may want to have a look at UIGestureRecognizerDelegate and then the method:

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

Upvotes: 1

Mutawe
Mutawe

Reputation: 6524

Try this :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  UITouch *touch = [touches anyObject];
      if ([touch view] == firstimage    ) { // Do Something}
      if ([touch view] == secondimage    ) { // Do Something}
 }

Upvotes: 0

Related Questions