【问题标题】:iPad: gesture recognizer on left/right side of screeniPad:屏幕左侧/右侧的手势识别器
【发布时间】:2016-04-15 05:48:53
【问题描述】:

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

实现这一点的最佳方法是什么?我可以在屏幕的左半边放一个手势识别器,在屏幕的右侧放另一个吗?即使双方同时被快速点击/滑动,两个单独的识别器是否会一起正常工作?或者我应该创建一个全屏识别器并完全自己解析滑动和点击?我没有使用手势识别器的经验,所以我不知道首选方法是什么,或者当您同时滑动多个手势时它们的工作效果如何。

【问题讨论】:

    标签: ios objective-c ipad uigesturerecognizer uiswipegesturerecognizer


    【解决方案1】:

    我最终在我的 GLKView 顶部制作了两个 UIView,一个在屏幕左侧,一个在右侧。每个视图都有一个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]];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多