如何处理多点触控

发布于 2024-12-05 17:28:51 字数 827 浏览 0 评论 0原文

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

    }
}

I am writing an app in xcode using box2d. Right now I am using the code below. The problem is that it will only handle one touch event. How can I make my code handle all of the touch events, in this case check the location of each touch. I also want to store the touches so that when they end I can use the proper code to end whatever the individual touches started.

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心不设防 2024-12-12 17:28:51

它应该是这样的:

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

It should be something like this:

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

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

NSSet *touchEvents = [event allTouches];

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

You can get the number of fingers touching the screen with:

NSSet *touchEvents = [event allTouches];

You can get each touches individual location, multi-taps, etc., using and enumerated for loop and stepping through touchEvents.

灵芸 2024-12-12 17:28:51

除了迭代触摸集之外,您还需要确保视图启用了多点触摸。这可以在 Interface Builder/Xcode 4 中完成

In addition to iterating through the set of touches, you'll need to make sure that the view is multi-touch enabled. This can be done in Interface Builder/Xcode 4

嘿哥们儿 2024-12-12 17:28:51

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

  }
}

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

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文