XCode - touchBegan - 最近的接触/新的接触

发布于 2024-09-25 23:10:43 字数 710 浏览 3 评论 0原文

我一直使用触摸开始跟踪多达8次触摸,并且每次都会触发一个事件。这些接触可以同时发生,也可以交错发生。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Began");
    NSSet *allTouches = [event allTouches];

    for (int i=0; i<allTouches.count; i++) {
        UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
        if (/*touch inside button in question*/) {
            //Trigger the event.    
        }
    }
}

该代码适用于多点触控,并且没有任何问题,除了:(看看你是否能猜到)

由于 allTouches 的工作方式,它实际上获得了所有触摸。因此,当用户开始另一次触摸时,它会循环遍历当前活动的所有触摸,从而触发其中一个按钮的事件两次。

例如:Johnny 按下按钮 1。事件 1 发生。 Johnny 将手指放在按钮 1 上,然后按下按钮 2。事件 2 发生,但按钮 1 仍然是 allTouches 的一部分,因此事件 1 再次被触发。

那么问题来了:我如何获得新的触感?

I have been using touches began to track as many as 8 touches, and each triggers an event. These touches can occur at the same time, or staggered.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch Began");
    NSSet *allTouches = [event allTouches];

    for (int i=0; i<allTouches.count; i++) {
        UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
        if (/*touch inside button in question*/) {
            //Trigger the event.    
        }
    }
}

That code works for the multitouch, and it has no problems, EXCEPT: (See if you can guess)

Due to the way allTouches works, it literally gets all of the touches. Because of this, it loops through all of the touches that are currently active when the user starts another touch, and thus triggers the event of one of the buttons twice.

Ex: Johnny is pressing button 1. Event 1 occurs. Johnny leaves his finger on button 1, and presses button 2. Event 2 occurs, BUT button 1 is still a part of allTouches, and so, event 1 is triggered again.

So here's the question: How do I get the new touch?

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

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

发布评论

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

评论(1

最冷一天 2024-10-02 23:10:43

对于任何连续触摸,后续调用 TouchBegan 时都将返回相同的触摸对象。因此,只需将您已经处理过的每个 UITouch *touch 保存为开始(尚未结束),并且当您下次在 TouchsBegan 中迭代时,跳过您保存/标记的那些。

The same touch object will be returned on subsequent calls to touchesBegan for any continuous touch. So just save each UITouch *touch that you have already handled as begun (and not yet ended), and as you iterate the next time in touchesBegan, skip the ones you've so saved/marked.

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