【发布时间】:2013-12-31 09:48:48
【问题描述】:
所以我正在做自定义手势识别,我遇到了麻烦。我需要监控所有当前的触摸,但touchesBegan、touchesEnded、touchesMoved 和touchesCancelled 都只给我他们正在监控的触摸。我需要对所有的触摸进行计算,不管它们是否被监控。我已经进行了一些研究,并看到了自己监控触摸的建议,但对我来说并不顺利。这是我的代码:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[myTouches unionSet:touches];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *objects = [myTouches allObjects];
for (UITouch *touch in touches) {
for (UITouch *touch2 in objects) {
if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
[myTouches removeObject:touch2];
[myTouches addObject:touch];
}
}
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[myTouches minusSet:touches];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[myTouches minusSet:touches];
NSArray *objects = [myTouches allObjects];
for (UITouch *touch in touches) {
for (UITouch *touch2 in objects) {
if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
[myTouches removeObject:touch2];
}
}
}
}
我想要的:myTouches 应该是屏幕上所有触摸的准确表示。
我得到了什么:myTouches 不断增长,尽管有时来自事件的触摸被正确注册为与myTouches 中的触摸相同。我还做了一个测试,我对touchesBegan 中注册的触摸次数进行整数计数,并减去touchesEnded 中注册的触摸次数,结果总是为正数,这意味着注册的触摸次数多于结束的次数。请帮忙!
【问题讨论】:
-
您是否也在考虑“touchesCancelled”?
-
我在
touchesCancelled:中有[myTouches minusSet:touches]。 -
UIEvent 的触摸数组与 NSSet
touches不同。我不得不使用这个数组来执行手动多点触控事件。我不确定它是否是完整的集合,但它比touches更完整。使用[event allTouches]获取该数组。 -
谢谢,原来这就是我需要做的。
allTouches似乎给了我屏幕上的所有触摸,这正是我所需要的。让它成为一个答案,我会接受它。
标签: ios objective-c touch uitouch uievent