所以我正在做自定义手势识别,我遇到了麻烦。我需要监控所有当前的触摸,但 touchesBegan 、touchesEnded 、touchesMoved 和 touchesCancelled 都只给我触摸他们正在监控。我需要对所有的触摸进行计算,不管它们是否被监控。我已经进行了一些研究,并看到了自己监控触摸的建议,但对我来说并不顺利。这是我的代码:
-(void)touchesBeganNSSet *)touches withEventUIEvent *)event {
/* Called when a touch begins */
[myTouches unionSet:touches];
}
-(void)touchesMovedNSSet *)touches withEventUIEvent *)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)touchesCancelledNSSet *)touches withEventUIEvent *)event {
[myTouches minusSet:touches];
}
-(void)touchesEndedNSSet *)touches withEventUIEvent *)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 中注册的触摸次数,结果总是为正数,这意味着更多的触摸被注册而不是结束。请帮忙!
Best Answer-推荐答案 strong>
尝试 UIApplication 的子类来监控应用程序中的所有触摸。
@interface MyUIApplication : UIApplication
然后在MyUIApplication 中使用事件sendEvent
- (void)sendEventUIEvent *)event {
[super sendEvent:event];
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
//To get phase of a touch
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan){
//Do the stuff
}
//also you can use UITouchPhaseMoved, UITouchPhaseEnded,
// UITouchPhaseCancelled and UITouchPhaseStationary
}
}
别忘了在 main 中添加类
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
}
}
关于ios - 获取所有当前的 UITouch,包括未更新的 UITouch,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/20855294/
|