开发者

one touch in two button in same time

开发者 https://www.devze.com 2023-04-05 05:31 出处:网络
My app has 2 buttons, near. I\'m trying to tou开发者_StackOverflow社区ch this two buttons, at the same time, using only one finger.

My app has 2 buttons, near.

I'm trying to tou开发者_StackOverflow社区ch 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?


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

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号