模拟的独立手势

时间:2016-07-27 09:14:10

标签: ios objective-c uigesturerecognizer

我正在制作一个游戏,玩家一次移动两个数字。每个人都有自己的一半屏幕,并在内部移动。不幸的是,我发现当我用两个拇指一次滑动时没有任何反应。甚至没有一个识别器被触发。

也许有一种方法。我在GameViewController的顶部再创建了两个视图,并添加了单独的手势。但是我无法在我的gamescene.m中为他们提供创造行动。

无论如何都要识别在GameScene中GameViewController中声明的滑动,并在其中添加任何动作?

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

-(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];

    }
}

3 个答案:

答案 0 :(得分:1)

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

在代表中,实现:

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

答案 1 :(得分:0)

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

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;
 }

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

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

答案 2 :(得分:0)

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