UISwipeGestureRecognizers

UISwipeGestureRecognizers

在我的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;
}

最佳答案

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

您可以通过以下操作解决此问题:

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

关于ios - UISwipeGestureRecognizers将不会同时被识别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35384193/

10-13 05:03