同时按下两个按钮

发布于 2024-12-05 04:25:36 字数 80 浏览 1 评论 0原文

我的应用程序有 2 个按钮,靠近。

我试图仅用一根手指同时触摸这两个按钮。

有没有办法检查这两个按钮是否同时触摸?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-12-12 04:25:36

你可以(但实际上不应该)像 Abizern 所说的那样用一根手指触摸 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
        }
    }

}

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

}

灯角 2024-12-12 04:25:36

一次触摸就是一个点。尽管建议制作合理大小的控件,但当您触摸屏幕时,您得到的是一个点,而不是一个区域。该点将位于一个或另一个控件中。

您可以尝试拦截触摸并将其变成更大的矩形,看看是否同时覆盖两个按钮。

编辑

看看这个SO问题 查看拦截触摸的一种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文