Iphone检测每个手指和画线(使用cocos2d / openGL进行绘图)

时间:2012-11-26 04:43:59

标签: iphone ios cocos2d-iphone

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

  1. 当一个文件触及视图/屏幕时,该视图将通过一组ccTouchesBegan收到UITouch
  2. UITouch将保留位置(CGPoint),并且每个手指都有唯一的位置。
  3. 如果同时有多个手指触摸视图,则会向视图发送2 UITouch
  4. 有时候视图会收到ccTouchesBeganUITouch次,ccTouchesBegan会被称为twise,每次手指触摸一个接一个。
  5. 如果finger1正在移动,则视图将会收到ccTouchesMoved一个UITouch
  6. 我的问题是如何用每个手指单独划线来画线,将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);
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

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