2016-07-27 62 views
1

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

也许有一种方法。我在GameViewController的顶部创建了另外两个视图并添加了单独的手势。但我无法在我的gamescene.m中向他们作出反应来创建动作。

是否有无论如何承认在GameViewControl中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]; 

    } 
} 

回答

0

最简单的解决方案是将屏幕分为两个较小的观点和附加单独的手势识别到每一个。

1

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

在委托,实施这样的:

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

我已经这样做了,但没有任何结果。将interface和bool中的delegate设置为YES,您也可以看到self.swipeGestureDown.delegate = self;在每个分配代码块中行。我认为它与其他一些识别器一样,比如双击或其他。 (我没有在我的代码中声明这样的事情) –

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