iPad:屏幕 left/right 侧的手势识别器
iPad: gesture recognizer on left/right side of screen
我正在开发一个基于 GLKViewController 的游戏,它将点击和滑动解释为游戏控制。我想通过让第一个玩家在屏幕左侧点击或滑动,让第二个玩家在屏幕右侧点击或滑动来支持双人游戏模式。在一个完美的世界中,我希望手势识别器能够工作,即使滑动很草率并且超过了屏幕的中心线(滑动的起点用于确定哪个玩家获得输入)。
实现它的最佳方法是什么?我可以在屏幕的左半边放置一个手势识别器,在屏幕的右侧放置另一个吗?即使双方同时快速 tapped/swiped ,两个独立的识别器能否一起正常工作?或者我应该创建一个全屏识别器并完全自己解析滑动和点击?我没有使用手势识别器的经验,所以我不知道首选方法是什么,也不知道当同时滑动多个手势时它们的效果如何。
我最终将两个 UIView 叠加在我的 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]];
}
我正在开发一个基于 GLKViewController 的游戏,它将点击和滑动解释为游戏控制。我想通过让第一个玩家在屏幕左侧点击或滑动,让第二个玩家在屏幕右侧点击或滑动来支持双人游戏模式。在一个完美的世界中,我希望手势识别器能够工作,即使滑动很草率并且超过了屏幕的中心线(滑动的起点用于确定哪个玩家获得输入)。
实现它的最佳方法是什么?我可以在屏幕的左半边放置一个手势识别器,在屏幕的右侧放置另一个吗?即使双方同时快速 tapped/swiped ,两个独立的识别器能否一起正常工作?或者我应该创建一个全屏识别器并完全自己解析滑动和点击?我没有使用手势识别器的经验,所以我不知道首选方法是什么,也不知道当同时滑动多个手势时它们的效果如何。
我最终将两个 UIView 叠加在我的 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]];
}