0

My app has 2 buttons, near.

I'm trying to touch this two buttons, at the same time, using only one finger.

There is a way to check if the touch is over this two buttons at same time?

Ratata Tata
  • 2,781
  • 1
  • 34
  • 49

2 Answers2

1

A touch is a point. Although the recommendation is to make controls of a reasonable size, when you touch the screen, you are getting a point, not a region. That point is going to be in one or other of the controls.

You could try to intercept the touch and turn it into a larger rectangle and see if that covers both the buttons at the same time.

EDIT

Have a look at this SO question to see one way of doing intercepting touches.

Community
  • 1
  • 1
Abizern
  • 146,289
  • 39
  • 203
  • 257
1

You can (but really shouldnt) touch 2 buttons with one finger like Abizern said, but you can also call 2 methods for one button touch. For example:

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

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

However, this is not always the correct behavior for what you're trying to do, so starting with iOS 3, we can use a bit of jiggering with the UITouch and event mechanism to figure a lot of it out:

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

}

CodaFi
  • 43,043
  • 8
  • 107
  • 153