如何处理多点触控

时间:2011-09-21 22:51:42

标签: iphone xcode box2d multi-touch

我正在使用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

    }
}

4 个答案:

答案 0 :(得分:1)

它应该是这样的:

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

答案 1 :(得分:1)

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

NSSet *touchEvents = [event allTouches];

您可以使用和枚举for循环以及单步执行touchEvents来触摸各个位置,多次点击等。

答案 2 :(得分:1)

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

中完成

答案 3 :(得分:0)

在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);

  }
}