【问题标题】:Get all current UITouches, including ones that aren't being updated获取所有当前的 UITouch,包括未更新的 UITouch
【发布时间】:2013-12-31 09:48:48
【问题描述】:

所以我正在做自定义手势识别,我遇到了麻烦。我需要监控所有当前的触摸,但touchesBegantouchesEndedtouchesMovedtouchesCancelled 都只给我他们正在监控的触摸。我需要对所有的触摸进行计算,不管它们是否被监控。我已经进行了一些研究,并看到了自己监控触摸的建议,但对我来说并不顺利。这是我的代码:

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


【解决方案1】:

尝试 UIApplication 的子类来监控应用程序中的所有触摸。

    @interface MyUIApplication : UIApplication

然后在MyUIApplication中使用事件sendEvent

    - (void)sendEvent:(UIEvent *)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]));
          }
    }

【讨论】:

  • sendEvent: 去哪儿了?这如何与 UIViewController 交互?
  • 由于 MyUIApplication 是 UIApplication 的子类,sendEvent: 将响应所有事件而与 UIViewController 无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多