2012-11-19 14 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