iOS模拟器会在随机时间段之后停止正确识别手势

时间:2018-01-31 21:18:52

标签: ios objective-c simulator uitapgesturerecognizer uipangesturerecognizer

抱歉这是多长时间。这对我来说是一个谜,寻找任何人可能有的任何指针。

iOS模拟器中似乎存在一个错误,即UIGestureRecognizers如何工作并传递持续存在的触点,直到模拟器重新启动。甚至杀死应用程序并在模拟器中重新启动它也无法修复它。修复它(暂时)的唯一方法是完全退出模拟器并重新启动它。

我在UIViewController子类中有一个SKView子类。 SKView子类占据UIViewController的底部,顶部包含一些按钮和其他控件。该游戏涉及水龙头和平底锅,由UIGestureControllers管理,由拥有SKScene子类的SKView子类拥有。

在任意时间段后,手势将停止正常工作。在SKScene拥有的视图中,没有一个被正确识别。如果我在SKView外部和父UIViewController中单击,它们会显示,但坐标都是错误的。它也会出现状态错误 - 有时UIPanGestureRecognizer会认为有两个手指按下而不是只有一根手指。 UIPanGestureRecognizer同样关闭。这种行为没有可识别的模式 - 它只是突然开始。

以下是初始化SKView及其场景的相关代码:

    SKView * _gameView = [[SKView alloc] initWithFrame : gameRect];
    _gameView.clipsToBounds = YES;
    // _gameView.scene is an SKScene subclass
    CGSize gameSize = CGSizeMake(_gameView.bounds.size.width,
                                 _gameView.bounds.size.width);
    _scene = [[GameScene alloc] initWithSize : gameSize];
    // Present the scene.
    [_gameView presentScene : _scene];
    [self.view addSubview : _gameView];

以下是注册UIGestureControllers的SKScene子类中的代码:

- (void) initializeGestureRecognition : (SKView *) view
{
    UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget : self action : @selector (handlePanGesture:)];
    UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleTapGesture:)];
    panRecognizer.delegate = self;
    tapRecognizer.delegate = self;
    panRecognizer.maximumNumberOfTouches = 2;
    [tapRecognizer requireGestureRecognizerToFail : panRecognizer];
    [view addGestureRecognizer : panRecognizer];
    [view addGestureRecognizer : tapRecognizer];
}

以下是UIPanGestureRecognizer的handlePanGesture委托的代码:

-(void) handlePanGesture : (UIPanGestureRecognizer*) panRecognizer
{
    self.deltaInView = [panRecognizer translationInView : self.view];
    self.deltaInScene = CGPointMake(deltaInView.x, -(deltaInView.y));
    if (UIGestureRecognizerStateChanged == panRecognizer.state)
        {
            self.panTouchPoint = [self getPanTouchPoint : panRecognizer];
        }
    else if (UIGestureRecognizerStateEnded == panRecognizer.state)
        {
            // Other non-relevant stuff here
        }
}

- (CGPoint) didGetPanTouchPoint : (UIPanGestureRecognizer*) panRecognizer
{
    CGPoint touchPoint = CGPointMake(0.0, 0.0);
    if (2 == panRecognizer.numberOfTouches)
    {
        CGPoint tempPoint = [self calculateSafeCentroidPointFromRecognizer : panRecognizer];
        touchPoint = [self convertPointFromView : tempPoint];
    }
    else if (1 == panRecognizer.numberOfTouches)
    {
        CGPoint tempPoint = [panRecognizer locationOfTouch : 0
                                                    inView : self.view];
        touchPoint = [self convertPointFromView : tempPoint];
    }
    return touchPoint;
}


- (CGPoint) calculateSafeCentroidPointFromRecognizer : (UIGestureRecognizer*) recognizer
{
    CGPoint thisPointInView = [recognizer locationOfTouch : 0
                                                   inView : self.view];
    CGPoint thatPointInView = [recognizer locationOfTouch : 1
                                                   inView : self.view];
    CGPoint safePoint = [self calculateCentroidPoint : thisPointInView
                                         secondPoint : thatPointInView];
    return safePoint;
}


CGPoint CalculateCentroidPoint(const CGPoint * thisPoint,
                               const CGPoint * thatPoint)
{
    CGPoint centroidPoint = CGPointMake((thisPoint->x + thatPoint->x) / 2.0),
                                        (thisPoint->y + thatPoint->y) / 2.0));
    return centroidPoint;
}

以下是UITapGestureRecognizer的handleTapGesture委托的代码:

- (void) handleTapGesture : (UIGestureRecognizer *) gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        UITapGestureRecognizer * tapRecognizer = (UITapGestureRecognizer*) gestureRecognizer;
        CGPoint viewTouchLocation = [tapRecognizer locationOfTouch : 0
                                                            inView : self.view];
        CGPoint sceneTouchLocation = [self convertPointFromView : viewTouchLocation];
        // Do other stuff here
    }
}

有一点需要注意的是,水龙头和平移手势识别器都是错误的。它不是一个或另一个。这让我相信存在一些系统性问题。

(或者,更有可能的是,我已经搞砸了。)

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

有时候模拟器会认为你按住了Option()键并且卡住了这个键,这意味着在屏幕上激活了捏合手势助手。要停止此操作,再次按下选项