UISwipeGestureRecognizers 不会被同时识别

UISwipeGestureRecognizers won't be recognized simultaneously

在我的 spritekit 游戏中,我试图让 2 个 UISwipeGestureRecognizers 可以同时被识别,但是,我无法这样做。任何帮助将不胜感激,在此先感谢。下面是我的代码...

在 ViewDidLoad 中:

UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwiped1:)];

[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];

UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwiped1:)];

[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];

ViewDidLoad 后:

-(void)rightSwiped1:(UIGestureRecognizer *)gestureRecognizer {

CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
    SKNode *person1 = [self childNodeWithName:@"person1"];
    SKAction *moveRight = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
    [person1 runAction:moveRight];
} else if (pt.x > (self.view.bounds.size.width/2)) {

    SKNode *person2 = [self childNodeWithName:@"person2"];
    SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
    [person2 runAction:moveRight2];
}


}
-(void)leftSwiped1:(UIGestureRecognizer *)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
    SKNode *person1 = [self childNodeWithName:@"person1"];
    SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
    [person1 runAction:moveLeft];
} else if (pt.x > (self.view.bounds.size.width/2)) {

    SKNode *person2 = [self childNodeWithName:@"person2"];
    SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
    [person2 runAction:moveLeft2];
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}

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

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}

您永远不会分配 UISwipeGestureRecognizersdelegate 属性,因此永远不会调用您的委托方法。

您只需执行以下操作即可解决此问题:

UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwiped1:)];

leftSwipe1.delegate = self;
[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];

UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwiped1:)];

rightSwipe1.delegate = self;
[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];

确保您也遵守 UIGestureRecognizerDelegate 协议:

@interface YourViewController <UIGestureRecognizerDelegate>

...

@end