2012-11-19 18 views
0

大家好!我需要在同一时间在不同的图像视图上检测2个触摸。所以我需要在用户同时触摸两个图像视图时启动计时器。触摸结束时停止定时器。图像视图正在屏幕上移动。当我使用一个图像视图时没有问题。你有什么想法吗?同时检测2个接触

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

感谢所有帮助))

+0

“两次同时触摸” - 同一时间延迟一秒钟?大约100ms呢?大概1分钟? –

回答

0

我想关于这个问题很多,我找到了如何做到这一点。 这里是我的代码:

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

试试这个:

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

你可能想看看UIGestureRecognizerDelegate,然后方法:

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 
0
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; 
} 

那么你识别器两个触摸的位置,并决定你做什么;

+0

我尝试过,但图像视图是动画的。这就是为什么我选择使用 - (void)touchesMoved:(NSSet *)触及事件:(UIEvent *)事件 –

+0

对不起,我不能帮你,那么我认为,当你使用触摸事件,为什么不使用清晰的视图,这是两个imageView的晚餐视图,然后你使用识别器的两个触摸位置;希望可以帮助你 – signal