Reputation: 103
In my current Sprite Kit project, I have coded a mechanic where I have employed a UISwipeGestureRecognizer for the left direction which is applied to two nodes. When a left swipe occurs on one side of the screen, one of the nodes moves and when a left swipe occurs on the other side of the screen, the other node moves. However, I cannot find a way to make the nodes move at the same time; 2 left swipes are not recognized although they are in different locations on the screen. Similarly, I have also coded a similar mechanic for when the user swipes to the right. Below is my code. I would greatly appreciate any help. As of now, only one node can be swipes at a time, not both together, which is what i would like
-(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];
}
}
Upvotes: 1
Views: 62
Reputation: 1981
This is should be help you
At first we should create class, that implements UIGestureRecognizerDelegate protocol:
@interface TestGestureDelegate : NSObject
@implementation TestGestureDelegate
(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES; }
(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
Also this is good source to learn about, Gesture
Upvotes: 1
Reputation: 4047
Try implementing this method:
- gestureRecognizer:shouldRecognizerSimultaneouslyWithGestureRecognizer:
It will prevent one of your gestures from blocking the other.
Also, make sure that you set your current ViewController, in which the implementation for this method will be, as the delegate of both of your UIGestureRecognizers, so that this method will actually get called.
I hope this helps. Good Luck!
Upvotes: 0