【问题标题】:Iphone Detect each Fingers and Draw Line (Using cocos2d/openGL for Drawing)Iphone 检测每个手指并画线(使用 cocos2d/openGL 进行绘图)
【发布时间】:2012-11-13 13:42:30
【问题描述】:

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

  1. 当手指触摸视图/屏幕时,该视图将收到ccTouchesBegan 和一组UITouch
  2. UITouch 将保存每个手指的位置 (CGPoint) 及其唯一性。
  3. 如果多个手指同时触摸视图,则会向视图发送 2 个UITouch
  4. 有时视图会收到ccTouchesBegan,其中2 个UITouchccTouchesBegan 将被调用,因为每个手指一个接一个地触摸。
  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);
    }

}

【问题讨论】:

    标签: iphone ios cocos2d-iphone


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多