TouchBegan & touchhesMoved

So, here is a strange question. I have a UIImageView that responds to touchesMoved: and touchesBegan: it works fine. The main thing I came across is if you put your finger down on image1, and then with that finger that is still on image1, you would press image2 with the other finger.

In turn, this would fire image1 again. I do not want this to happen. I want to be able to use Multi Touch while pressing both images at the same time, rather than repeating them again and again. There must be something that I must add so that this does not happen.

the code:

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; for (UITouch *touch in allTouches) { CGPoint location = [touch locationInView:touch.view]; if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) { //Play Sound NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &soundID); AudioServicesPlaySystemSound (soundID); // lastButton = image1; } if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) { //Play Sound NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &soundID); AudioServicesPlaySystemSound (soundID); // lastButton = image2; } } 

And for touches

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView:touch.view]; if(CGRectContainsPoint(image1.frame, location) && lastButton != image1) { //Play Sound NSString *path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &soundID); AudioServicesPlaySystemSound (soundID); // lastButton = image1; } if(CGRectContainsPoint(image2.frame, location) && lastButton != image2) { //Play Sound NSString *path = [[NSBundle mainBundle] pathForResource:@"sound2" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path] , &soundID); AudioServicesPlaySystemSound (soundID); // lastButton = image2; } } 

Finally, here are the finals:

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { lastButton = nil; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { [self touchesEnded:touches withEvent:event]; } 

So I just need to do this if I hold one of the images and then click the other, release it again and click on it again so that it stops shooting at the first image that I click on.

+6
objective-c iphone cocoa-touch xcode
source share
2 answers

All views considered will receive all touches (on NSSet , as mentioned by Jason). It depends on each species (up to you, that is) that you have chosen that interests you. Therefore, you need to check whether each touch is within the limits of the view, and try to match the touch with the last. Since you get strokes in a set, you cannot count on their order to calculate how much they moved, for example. You must save the coordinates of the last touch and select the closest touch and assume that he moved the same finger.

+1
source share

Use the NSSet strokes that are passed to your touchesBegan: method, rather than allTouches: from the event.

0
source share

All Articles