2011-09-16 64 views
0

我的應用程序有2個按鈕,在附近。一鍵同時在兩個按鈕

我試圖只用一根手指同時觸摸這兩個按鈕。

有一種方法可以檢查觸摸是否同時在這兩個按鈕上?

+0

您是否嘗試過使用UITapGestureRecognizer用自來水的數量是一個。 – user523234

回答

1

您可以(但真的不應該)用Abagen說的一根手指觸摸2個按鈕,但您也可以調用2個方法來觸摸一個按鈕。例如:

-(void)viewDidLoad { 
    self.myButton = /*initialize the button*/; 
    [self.myButton addTarget:self action:@selector(callTwoMethods) forControlEvents:UIControlEventTouchUpInside]; 
} 

-(void)callTwoMethods { 
    [self methodOne]; 
    [self methodTwo]; 
} 

然而,這並不總是你想要做什麼,所以開始與iOS 3正確的行爲,我們可以使用與UITouch和事件機制炮製一成不變的位推測一很多出來:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if ([touches count] == 2) { 
     //we are aware of two touches, so get them both 
     UITouch *firstTouch = [[allTouches allObjects] objectAtIndex:0]; 
     UITouch *secondTouch = [[allTouches allObjects] objectAtIndex:1]; 

     CGPoint firstPoint = [firstTouch locationInView:self.view]; 
     CGPoint secondPoint = [secondTouch locationInView:self.view]; 

     if ([self.firstButton pointInside:firstPoint withEvent:event] && [self.secondButton secondPoint withEvent:event] || /*the opposite test for firstButton getting the first and secondButton getting the second touch*/) { 
      //Do stuff 
     } 
    } 

}

1

觸摸是一個點。雖然建議是控制一個合理的大小,但當你觸摸屏幕時,你會得到一個點,而不是一個區域。這一點將在一個或其他控制。

您可以嘗試攔截觸摸並將其變成更大的矩形,並查看是否同時覆蓋了兩個按鈕。

編輯

看一看這個SO question看到做攔截觸摸的一種方式。

+0

正是我想要做的。攔截觸摸並將其變爲更大的矩形。 –

相關問題