Reputation: 17
In my current project, I am employing a UITapGestureRecognizer. I have coded it so that when the left side of the screen is tapped, an object moves, and when the right side of the screen is tapped, another object moves. However, the gesture recognizer is only working when the right side is tapped. Also, I would like to make it so that the SKActions can be executed if both sides are tapped simultaneously. I don't understand what I am doing wrong. My code is below. Thanks in advance
In ViewDidLoad
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
[tap1 setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap1];
view.multipleTouchEnabled = YES;
-(void)tap1:(UIGestureRecognizer *)gestureRecognizer {
SKNode *person11 = [self childNodeWithName:@"person1"];
SKNode *person2 = [self childNodeWithName:@"person2"];
CGPoint pt = [gestureRecognizer locationInView:self.view];
if (pt.x > (self.view.bounds.size.width/2))
{
if (person2.position.x == CGRectGetMidX(self.frame) + 400 ) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveLeft2];
}
if (person2.position.x == CGRectGetMidX(self.frame) + 90 ) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveRight2];
}
}
if (pt.x < (self.view.bounds.size.width/2)){
if (person11.position.x == CGRectGetMidX(self.frame) - 90 ) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveLeft2];
}
if (person11.position.x == CGRectGetMidX(self.frame) - 400 ) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveRight2];
}
}
}
Upvotes: 0
Views: 85
Reputation: 3597
Instead of a gesture recognizer, do the work in touchesEnded. Here's an example:
Swift
override func viewDidLoad() {
super.viewDidLoad()
self.view.multipleTouchEnabled = true
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if(touch.locationInView(self.view).x<self.view.frame.midX) {
print("On the left half") // run the respective SKAction
} else {
print("On the right half") // run the respective SKAction
}
}
}
Objective C (should work, but I use Swift and translated on my phone, so this might not work perfectly)
-(void)viewDidLoad {
self.view.multipleTouchEnabled = YES
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UITouch* touch in touches) {
if(touch.locationInView(self.view).x<self.view.frame.midX) {
printf("On the left half\n"); // run the respective SKAction
} else {
printf("On the right half\n"); // run the respective SKAction
}
}
}
This will do exactly what you want.
Pretty self explanatory: allows multiple touches, then, when the touch is released, it checks its relative horizontal location in the view (.x
), and sees if it's on the left or right half of the view. No gesture recognizers needed!
I'll try to double check the Objective C code later, but the Swift definitely works.
If you have any questions, please tell me!
Upvotes: 1
Reputation: 65
Try to put this (in Swift):
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
Sorry I used Swift frequently... I guess you can simply find an built-in gestureRecognizer() on Objective-C too.
Upvotes: 0