我的应用程序有两个按钮,近旁。
我试着用一根手指同时触摸这两个按钮。
有没有办法检查触摸是否同时在这两个按钮上?
发布于 2011-09-17 05:28:54
你可以(但确实不应该)像Abizern说的那样用一根手指触摸两个按钮,但你也可以调用两个方法来实现一个按钮的触摸。例如:
-(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
}
}}
https://stackoverflow.com/questions/7449896
复制相似问题