2012-05-31 59 views
0

我已經做了一個應用程序,我想讓它多點觸控兼容。我試過四處尋找,但答案並不是特定於我的。 這裏是我做的:如何使多點觸控啓用iOS應用程序

1)我的編碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

[self touchesMoved:touches withEvent:event]; 
if (gameState == kGameStatePaused) {(gameState = kGameStateRunning);} 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
if(gameState == kGameStateRunning) { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 
    if(location.x > 400) { 

     CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y); 
     playerPaddle.center = yLocation; 
    } 

    if (gameStyle == kGameStyleTwoP) { 
     if(location.x < 100) { 

      CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y); 
      computerPaddle.center = yLocation2; 
     } 
    } 
} 

2)我已經到界面生成器和檢查多點觸控啓用

3盒)我建立並運行我的應用程序並打開正確,當我去測試多點觸控我按住「選項鍵」,然後點擊並移動鼠標

4)(我正在試圖讓computerPaddle和playerPaddle都移動),但只有一個作品在時間

我必須嘗試解決它,但我不明白我要去哪裏錯了。

任何幫助都很有用。 THX。

回答

2

看這條線

UITouch *touch = [[event allTouches] anyObject]; 

你只需要一點觸而忽略休息,這就是爲什麼只有一兩件事可以移動

用一個for循環,以便更換就可以解決問題

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
if(gameState == kGameStateRunning) { 

    for (UITouch *touch in [event allTouches]) { 
    CGPoint location = [touch locationInView:touch.view]; 
    if(location.x > 400) { 

     CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y); 
     playerPaddle.center = yLocation; 
    } 

    if (gameStyle == kGameStyleTwoP) { 
     if(location.x < 100) { 

      CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y); 
      computerPaddle.center = yLocation2; 
     } 
    } 
} 
} 
10

UIView上有一個名爲multipleTouchEnabled的屬性,您可以將其設置爲YES以啓用它(默認爲NO)。

此外,您應該循環處理touchesMoved中收到的touches集合中的所有接觸。

+0

我認爲你有正確的想法,但xlc0212給了編碼。無論如何 – burkel

相關問題