【问题标题】:How to handle multitouch如何处理多点触控
【发布时间】:2011-09-21 22:51:41
【问题描述】:

我正在使用 box2d 在 xcode 中编写应用程序。现在我正在使用下面的代码。问题是它只会处理一个触摸事件。如何让我的代码处理所有触摸事件,在这种情况下检查每个触摸的位置。我还想存储触摸,以便当它们结束时,我可以使用正确的代码来结束个人触摸开始的任何内容。

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    CGSize screenSize = [CCDirector sharedDirector].winSize;

    if (locationWorld.x >= screenSize.width*2/5/PTM_RATIO && locationWorld.x <= screenSize.width*3.25/5/PTM_RATIO) {
        //do something
    }
    else if (0 && locationWorld.x <= screenSize.width*2/5/PTM_RATIO) {
        //do something else

    }
}

【问题讨论】:

    标签: iphone xcode box2d multi-touch


    【解决方案1】:

    应该是这样的:

    - (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        for (UITouch *touch in touches) 
        {
            if (touch.phase == UITouchPhaseBegan) 
            {
                // Insert code here
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式获取触摸屏幕的手指数:

      NSSet *touchEvents = [event allTouches];
      

      您可以使用和枚举 for 循环和单步执行 touchEvents 来获取每个触摸的单独位置、多次点击等。

      【讨论】:

        【解决方案3】:

        除了遍历一组触控之外,您还需要确保视图启用了多点触控。这可以在 Interface Builder/Xcode 4 中完成

        【讨论】:

          【解决方案4】:

          在 COCOS2D-X 中

          void LayerHero::ccTouchesEnded(CCSet* touches, CCEvent* event)
          {
          CCTouch* touch = (CCTouch*)( touches->anyObject() );
          CCPoint location = touch->getLocationInView();
          location = CCDirector::sharedDirector()->convertToGL(location);
          CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
          
          if(location.x<visibleSize.width/2)
          {
          
          }
           else if(location.x>visibleSize.width/2)
           {
              CCLOG("We are in the touch2 %f",location.x);
          
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-10-17
            • 1970-01-01
            • 2016-04-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多