在我目前的Sprite Kit项目中,我编写了一个机制,我已经将UISwipeGestureRecognizer用于向左方向应用于两个节点。当在屏幕的一侧出现左侧滑动时,其中一个节点移动,而在屏幕另一侧出现左侧滑动时,另一个节点移动。但是,我找不到一种方法使节点同时移动;尽管屏幕上位于不同的位置,但仍然无法识别2个左侧滑动。同样,我也为用户向右滑动时编写了类似的机制。以下是我的代码。我将不胜感激任何帮助。截至目前,只有一个节点可以在同一时间内刷卡,不能两者都在一起,这是我想如何根据触摸的位置获得两个完全相同的UISwipeGestureRecognizer以同时工作?
-(void)didMoveToView:(SKView *)view {
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];
self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;
}
-(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) - 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[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.2f];
[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.2f];
[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) + 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person2 runAction:moveLeft2];
}
}
你为什么不尝试直接申请的UISwipeGestureRecognizer到SKNode? –
我该怎么做? – RT33