同时独立的手势

Simulatenous independent gestures

我正在制作一个玩家一次移动两个数字的游戏。每个人都有自己的一半屏幕,并在其中移动。不幸的是,我发现当我同时用两个拇指滑动时,什么也没有发生。甚至我的识别器都没有被触发。

也许有一种方法。我在 GameViewController 的顶部制作了另外两个视图并添加了单独的手势。但我无法在我的 gamescene.m 中引用它们来创建操作。

有没有办法识别在 GameViewController 和 GameScene 中声明的滑动并向它们添加任何动作?

我已经尝试根据触摸的开始和结束制作我自己的识别器,但是当同时释放两个手指时它变得混乱并且通常忘记反应两次,我的意思是每次释放分别。

-(void)setUpGestActions{
    _swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)];
    [self.swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    self.swipeGestureLeft.cancelsTouchesInView = NO;
    self.swipeGestureLeft.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureLeft];

    _swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)];
    [self.swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
    self.swipeGestureRight.cancelsTouchesInView = NO;
    self.swipeGestureRight.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureRight];

    _swipeGestureUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
    [self.swipeGestureUp setDirection:UISwipeGestureRecognizerDirectionUp];
    self.swipeGestureUp.cancelsTouchesInView = NO;
    self.swipeGestureUp.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureUp];

    _swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
    [self.swipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
    self.swipeGestureDown.cancelsTouchesInView = NO;
    self.swipeGestureDown.delegate = self;
    [self.view addGestureRecognizer: self.swipeGestureDown];

    _moveLeft = [SKAction moveByX:-self.frame.size.width/6 y:0 duration:self.velocity];
    _moveRight = [SKAction moveByX:self.frame.size.width/6 y:0 duration:self.velocity];
    _moveUp = [SKAction moveByX:0 y:self.frame.size.width/6  duration:self.velocity];
    _moveDown = [SKAction moveByX:0 y:-self.frame.size.width/6 duration:self.velocity];

    _downMovement = [SKAction moveByX:0 y:-1 duration:self.downMovementVelocity];
}

-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveLeft];
    }
    else{
        [_girl runAction:self.moveLeft];

    }
}

-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveRight];
    }
    else{
        [_girl runAction:self.moveRight];
    }
}

-(void)swipeUp:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveUp];
    }
    else{
        [_girl runAction:self.moveUp];
    }
}

-(void)swipeDown:(UISwipeGestureRecognizer*) recognizer{
    _sideDisting = [recognizer locationInView:self.view];
    if(self.sideDisting.x <= self.frame.size.width/2){
        [_boy runAction:self.moveDown];
    }
    else{
        [_girl runAction:self.moveDown];

    }
}

要同时识别多个手势,请在每个手势识别器上设置一个委托。每个手势的委托可以是同一个对象。

在委托中,实现这个:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
     shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

我想你可以看看这个例子。 可能对你有帮助。

UIScreenEdgePanGestureRecognizer *myScreenEdgePanGestureRecognizer;
...
myScreenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgePan:)];
myScreenEdgePanGestureRecognizer.delegate = self;
// Configure the gesture recognizer and attach it to the view.
...
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    BOOL result = NO;
    if ((gestureRecognizer == myScreenEdgePanGestureRecognizer) && [[otherGestureRecognizer view] isDescendantOfView:[gestureRecognizer view]]) {
        result = YES;
    }
    return result;
 }

通过此 link,您将找到更多信息。

https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html

最简单的解决方案是将屏幕分成两个小视图,并为每个小视图附加单独的手势识别器。