XCode - touchBegan - 最近的接触/新的接触
我一直使用触摸开始跟踪多达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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何连续触摸,后续调用 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.