iPad:屏幕左/右侧的手势识别器

时间:2016-04-15 05:48:54

标签: ios objective-c ipad uigesturerecognizer uiswipegesturerecognizer

我正在开发一款基于GLKViewController的游戏,它将点按和滑动解释为游戏控制。我希望通过让第一个玩家点击或滑动屏幕左侧来支持双人模式,让第二个玩家点击或滑动屏幕右侧。在一个完美的世界中,我希望手势识别器能够工作,即使滑动很邋and并越过屏幕的中心线(滑动的起点用于确定哪个玩家获得输入)。

实施此操作的最佳方法是什么?我可以在屏幕的左半部分放置手势识别器,在屏幕的右侧放置另一个吗?即使双方同时快速拍打/刷卡,两个独立的识别器是否能正常工作?或者我应该创建一个全屏识别器并完全自行解析滑动和点击?我没有手势识别器的经验,所以当你同时刷过多个时,我不知道首选方法是什么或者它们的工作效果如何。

1 个答案:

答案 0 :(得分:0)

我最终将两个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]];
}