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

Iphone检测每个手指并绘制线条(使用cocos2d/openGL绘制),iphone,ios,cocos2d-iphone,Iphone,Ios,Cocos2d Iphone,我了解多点触控活动的基本原理 当figer触摸视图/屏幕时,该视图将收到带有一组UITouch的cctouchsbegind UITouch将保留位置(CGPoint)及其对每个手指的唯一性 如果多个手指同时接触视图,则2UITouch将被发送到视图 有时,view会收到带有2个UITouch的cctouchsbegind,或者cctouchsbegind会为每个手指触摸依次调用两次 如果finger1正在移动,视图将接收带有一个UITouch的ccTouchesMoved 我的问题是如何分别用

我了解多点触控活动的基本原理

  • 当figer触摸视图/屏幕时,该视图将收到带有一组
    UITouch
    cctouchsbegind
  • UITouch
    将保留位置(
    CGPoint
    )及其对每个手指的唯一性
  • 如果多个手指同时接触视图,则2
    UITouch
    将被发送到视图
  • 有时,view会收到带有2个
    UITouch
    cctouchsbegind
    ,或者
    cctouchsbegind
    会为每个手指触摸依次调用两次
  • 如果finger1正在移动,视图将接收带有一个
    UITouch的
    ccTouchesMoved
  • 我的问题是如何分别用手指触摸画线,将1或2个手指放在屏幕上,并为每个手指触摸开始/移动/结束画线

    以下代码在只有单触时有效,但对于多触时,由于上述第3点和第4点的原因,无法工作

    就这样


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

    -(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);
        }
    
    }