2016-02-13 82 views
0

在我的spritekit游戏中,我试图让它能够同时识别2个UISwipeGestureRecognizers,但是,我无法这样做。任何帮助将非常感谢,事先感谢。下面是我的代码...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; 
} 
+0

请告诉我们您的意思是同时。他们永远不会完全在同一时刻,因为那些是基于目标操作的识别器,而主UI线程是串行的。所以只会有一个方法立刻开始。 如果您的意思是在识别之间存在显着的延迟,您应该尝试@ originaluser2的解决方案,并尝试使用'cancelsTouchesInView'或'delaysTouchesBegan'属性 – Alistra

+0

我希望左右滑动操作同时发生if ,我会怎么做?如果在同一时间在屏幕两侧滑动,我希望指定的动作发生 – d33

回答

1

你永远不指定您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 
+0

感谢您的帮助,但由于某些原因,两个手势仍然不能同时识别,我不知道我是什么做错了 – d33