UISwipeGestureRecognizer in a drawing application

I am working on a simple note taking / drawing application. I have a view controller that works like paper / canvas. In it, I want to implement a UISwipeGestureRecognizer, for example, when a user quickly wants to show a menu that he or she can move up, instead of pressing the "change" button, or maybe scroll left or right when he / she wants to change paper / note.

The problem is that when the user tries (with two fingers), the application also draws a short line in the same direction.

I implemented UISwipeGestureRecognizer as follows:

UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; recognizer.numberOfTouchesRequired = 2; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; // Delegate method - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; } // Helper method for handeling swipes -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { if (recognizer.direction == UISwipeGestureRecognizerDirectionUp) { [self enableEditingMode]; } } 

The painting mechanism is processed in one controller in the following ways:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

It seems that the three methods above are being called before my UIGestureRecognizer. I tried to create a BOOL variable and disable the drawing code if the boolean variable is YES. But that did not work. Any other ideas on how to solve this problem would be great!

Note. I am testing napkins in a simulator. Maybe this affects it?

+4
source share
3 answers

I could make it work. We tested it only in the simulator, although without knowing whether it will work on the device, since the simulator instantly recognizes two finger touches. The solution looks like this (from http://www.iphonedevsdk.com/forum/iphone-sdk-development/23537-implemented-touchesbegan-only-giving-me-one-touch.html ):

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [touches setByAddingObjectsFromSet:[event allTouches]]; if (allTouches.count== 1) { if (isUsingPen) { [self painting:touches]; } if (isUsingEraser) { [self erasing:touches]; } } } 

Will I notify if this works on the device, or has anyone else tried this approach on the device?

0
source

You need to make sure that you also implement

 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; 

In this case, you can cancel the touch.

+2
source

In my opinion, registering gesture and touch recognition methods * is not a good idea. They are required to interfere with each other. You should only register touch * methods and determine if it was a scroll. Do not draw if it has been drawn in the right direction.

0
source

All Articles