3

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

  1. 当手指触摸视图/屏幕时,该视图将收到ccTouchesBegan一组UITouch.
  2. UITouch将保存CGPoint每个手指的位置 ( ) 及其唯一性。
  3. 如果多个手指同时触摸视图,则将 2 个手指UITouch发送到视图。
  4. 有时 view 会收到ccTouchesBegan 2 UITouch,或者ccTouchesBegan会因为每个手指一个接一个地触摸而被调用 twise 。
  5. 如果 finger1 正在移动视图,将收到ccTouchesMoved一个UITouch

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

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

正是这样 在此处输入图像描述

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

}
4

1 回答 1

1

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

于 2012-11-26T07:44:59.713 回答