0

我正在研究基於GLKViewController的遊戲,該遊戲將水龍頭和滑動解釋爲遊戲控制。我想通過讓第一個玩家在屏幕左側輕點或滑動,並讓第二個玩家在屏幕右側輕擊或滑動來支持雙人模式。在一個完美的世界裏,即使滑動是sl and的,並且通過屏幕的中心線(用滑動的起始點來確定哪個玩家獲得輸入),我還是希望手勢識別器能夠工作。iPad:屏幕左側/右側的手勢識別器

什麼是最好的實施方式?我可以在屏幕左半邊放置一個手勢識別器,另一個放在屏幕右側?即使雙方在同一時間被輕拍/快速掃描,兩個單獨的識別器是否能夠正常工作?或者我應該創建一個全屏幕識別器,並且完全依靠我自己來解析滑動和輕敲?我沒有手勢識別器的經驗,所以我不知道最好的方法是什麼,或者當你同時刷多個手勢時它們的工作效果如何。

回答

0

我最終將兩個UIViews疊加在我的GLKView的頂部,一個在屏幕的左側,另一個在右側。每個視圖有一個UIPanGestureRecognizer和一個UILongPressGestureRecognizer(長按識別器基本上是一個更靈活的敲擊 - 我需要用它來拒絕某些手勢同時被解釋爲平移和敲擊)。這工作很有成效。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Add tap and pan gesture recognizers to handle game input. 
    { 
     UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSidePan:)]; 
     panRecognizer.delegate = self; 
     [self.leftSideView addGestureRecognizer:panRecognizer]; 

     UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSideLongPress:)]; 
     longPressRecognizer.delegate = self; 
     longPressRecognizer.minimumPressDuration = 0.0; 
     [self.leftSideView addGestureRecognizer:longPressRecognizer]; 
    } 
    { 
     UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSidePan:)]; 
     panRecognizer.delegate = self; 
     [self.rightSideView addGestureRecognizer:panRecognizer]; 

     UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSideLongPress:)]; 
     longPressRecognizer.delegate = self; 
     longPressRecognizer.minimumPressDuration = 0.0; 
     [self.rightSideView addGestureRecognizer:longPressRecognizer]; 
    } 
} 

- (void)handleLeftSidePan:(UIPanGestureRecognizer *)panRecognizer 
{ 
    [self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[0]]; 
} 

- (void)handleRightSidePan:(UIPanGestureRecognizer *)panRecognizer 
{ 
    [self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[1]]; 
} 

- (void)handleLeftSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer 
{ 
    [self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[0]]; 
} 

- (void)handleRightSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer 
{ 
    [self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[1]]; 
} 
相關問題