2012-11-26 69 views
3

我明白的多點觸摸事件是如何工作的iPhone檢測每個手指和畫線(使用繪圖的cocos2d/OpenGL模式)

  1. 當菲格爾觸摸視圖/屏幕查看將收到ccTouchesBegan有基礎一套UITouch
  2. UITouch將保存位置(CGPoint)及其每個手指的唯一位置。
  3. 如果多個手指同時觸摸視圖,則會將2 UITouch發送到視圖。
  4. 某些時候查看會收到ccTouchesBegan與2 UITouchccTouchesBegan將被稱爲twise爲每個手指接觸一個接一個。
  5. 如果finger1正在移動視圖將收到ccTouchesMoved與一個UITouch

我的問題是如何分別用每個手指觸摸畫線,把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

您可以嘗試的工作將觸摸位置數組與不同的觸摸相關聯(類似於NSDictionary,UITouches作爲鍵,NSArrays作爲值)。然後,如果您的draw方法使用ccDrawLine或任何其他方式,則可以繪製這些線。只要不要忘記將這些陣列存儲在當前觸摸結束的地方。