Iphone检测每个手指并画线(使用cocos2d/openGL绘制)

标签 iphone ios cocos2d-iphone

我了解多点触控事件的工作原理

  1. 当手指触摸 View /屏幕时,该 View 将收到带有一组 UITouchccTouchesBegan
  2. UITouch 将保留每个手指的位置 (CGPoint) 及其唯一性。
  3. 如果多个手指同时触摸 View ,将向 View 发送 2 个 UITouch
  4. 有时 View 将收到带有 2 个 UITouchccTouchesBegan,或者 ccTouchesBegan 将针对每个手指触摸一个接一个地调用两次。<
  5. 如果 finger1 正在移动, View 将通过一个 UITouch 接收到 ccTouchesMoved

我的问题是如何分别用每个手指触摸画线,将 1 或 2 个手指放在屏幕上并为每个手指触摸开始/移动/结束画线?

以下代码在只有单点触摸时有效,但对于多点触摸,由于上述第 3 点和第 4 点,它不会工作。

就是这样 enter image description here

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 0) {

        // handle multi touch
        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
        CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
        touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];

        Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
        [[temEdge1 end] updateXY:touchLocation1];
        [[temEdge1 start] updateXY:touchLocation1];

        if ([touches count] > 1) {
            UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
            CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
            touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];

            Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
            [[temEdge2 end] updateXY:touchLocation2];
            [[temEdge2 start] updateXY:touchLocation2];

        }

    }
}

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

    if ([touches count] > 0) {

        // handle multi touch
        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
        CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
        touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];

        Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
        [[temEdge1 end] updateXY:touchLocation1];


        if ([touches count] > 1) {
            UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
            CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
            touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];

            Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
            [[temEdge2 end] updateXY:touchLocation2];
        }

    }

}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 0) {

        // handle multi touch
        UITouch *touch1 = [[touches allObjects] objectAtIndex:0];
        CGPoint touchLocation1 = [touch1 locationInView: [touch1 view]];
        touchLocation1 = [[CCDirector sharedDirector] convertToGL: touchLocation1];

        Edge *temEdge1 = (Edge*)[temEdges objectAtIndex:0];
        [[temEdge1 end] updateXY:touchLocation1];

        if ([touches count] > 1) {

            UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
            CGPoint touchLocation2 = [touch2 locationInView: [touch2 view]];
            touchLocation2 = [[CCDirector sharedDirector] convertToGL: touchLocation2];

            Edge *temEdge2 = (Edge*)[temEdges objectAtIndex:1];
            [[temEdge2 end] updateXY:touchLocation2];
        }
    }

}
-(void)draw
{
    [super draw];
    glLineWidth(5.f);
    ccDrawColor4B(0, 0, 255, 255);
    for (Edge *temEdge in temEdges) {
        CGPoint start = [[temEdge start] toCCP];
        CGPoint end   = [[temEdge end] toCCP];
        ccDrawLine(start , end);
    }

}

最佳答案

您可以尝试将触摸位置数组与不同的触摸相关联(类似于 NSDictionary,其中 UITouches 作为键,NSArrays 点作为值)。然后,您可以使用 ccDrawLine 或任何其他方式(如果您的 draw 方法)绘制这些线。只是不要忘记将这些数组存储在当前触摸结束的地方。

关于Iphone检测每个手指并画线(使用cocos2d/openGL绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558773/

相关文章:

cocos2d-iphone - Cocos2D : Stop/Resume CCAction on CCSprite

ios - 围绕 anchor 旋转 Sprite

ios - 如何在 iOS 7 上的我的应用程序中添加(撰写评论)/(评价此应用程序)

iphone - 图像未显示在 Interface Builder/iPhone 的 UIImageView 中

ios - UIViewController 没有接收到 TouchBegan 消息

ios - 如何在 ios 中的同一个 Nsmutablearray 中存储旧值和新值

ios - 如何正确禁用 UIPickerView 组件滚动?

ios - twilio iOS Sdk 中的 Twilio 来电问题

iphone - 如果一个方法接受对象并添加到数组中,是否应在必要时检测并释放该对象?

iphone - 在应用程序购买中:应用程序处于事件状态,并且仍显示[环境:沙箱]