iPad - Simultaneous recognition of the same gesture - ipad

I need have a view where users can draw multiple rectangles. Now I need to be able to move those rectangles using a Pan gesture. The problem I am having is that I can move any single rectangle with the Pan gesture recognizer with no problem. Now when I use 2 fingers to Pan 2 rectangles to move simultaneously it doesn't work.
Seem to me that the problem is the target/action that I specified for the pan gesture gets fired only once.
My view adopts the UIGestureRecognizerDelegate and has defined this delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Thanks in advance!

Add gesture recognizer to each created rectangle view individually by making a method.
like this
- (void)addGestureRecognizersToPiece:(UIView *)piece
{
// Add the gesture recognizer to the piece here
}
You can loop in to add the gesture recognizer to all the pieces simultaneously, or while u create each rectangle, just fire this method after creating the rectangle, and passing that as the piece parameter to this function.
That should help.

Related

2 simultaneous UILongPressGestureRecognizer

I want that when the user has two fingers touching the screen, two separate UILongPressGestureRecognizers recognize the gesture for each finger (1 recognizer for 1 finger). I could use a single UILongPressGestureRecognizer with numberOfTouchesRequired set to 2, but I need the 2 touches to be recognized independently (ex: I can release one finger and the other recognizer will remain active, which is not possible with a single gesture recognizer).
Thanks for your help.
To allow both gestures to work together, implement the following delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
To make it so that the long press has first priority, do:
[tapGesture requireGestureRecognizerToFail:longPress];

UIGestureRecognizer working on parent class but not on subclass

So I'm working on an app at work where a majority of the code written has been written by other people. So I have a class called BaseDetailViewController and another class which subclasses BaseDetailViewController called ProductViewController. I have a pinch recognizer on ProductViewController and there is a pinch recognizer on it's parent BaseDetailViewController. It seems like the recognizer on the parent seems to be overriding the recognizer on ProductViewController. When I comment out the one on the parent then the recognizer on the child works just fine.
I tried using:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return TRUE;
}
with proper delegation to see if that helped but the recognizer on BaseDetailViewController seems to take the bacon every time.
What do I need to do to get the one on ProductViewController firing?
It means you are trying to have two Pinch gesture recognizers on the same view. When you are adding pinch gesture recognizer in ProductViewController, try to invalidate pinch gesture recognizer in BaseDetailViewController. It should solve the problem.

Resolve multigesture conflicts for moving an object

I'm having a rectangle (derived from UIView). This rectangle has an attached UITapGestureRecognizer, UIPanGestureRecognizer, UIPinchGestureRecognizer and an UIRotationGestureRecognizer.
These gesture recognizers are used to move the view, zoom it and rotate it.
However there is a conflict between the recognizers since rotating doesn't work while panning and so on...
I tried the following
[self.pincher requireGestureRecognizerToFail:self.panner];
With this rotating working while panning. How to extend that such the pinching is working either?
Edit:
Whats working:
Every gesture recognizer is working on it's own. But in combination there are troubles or let's say I want a different behavior ;) If I pan an object and tap with a second finger on it and pinch, I want to zoom the object - this is not working.
However, starting rotating with the second finger works with the code line above.
The UIGestureRecognizerDelegate protocol declares the following method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
You can override this method and return YES to have your recognizers working at the same time:
self.pincher.delegate = self;
self.panner.delegate = self;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
EDIT:
I've written a simple view controller that implements simultaneous scale, rotate, and pan on a UIView, you can check out this gist

Cancel gesture recognizer on button action

I'm working on an iPad app. I have a view in which the user can draw with his finger. This view has a subview, which is a calculator, and which have buttons.
I would like that when the user touch a button, the superview (in which the user can draw) doesn't take into account this touch. (so the user doesn't draw when he touches the calculator)
Preferably, I would like to not change the code of the calculator view and the code of my superview. I have only access to them via properties of another class.
Is there a way to solve the problem please? I have tried exclusiveTouch, but it doesn't work.
If you have access to the button action and the drawing gesture you can simply set:
gesture.enabled = NO;
To cancel the current gesture processing and / or prevent it from starting. When you want to reenable the gesture depends on what type it is and how it's used but doing it immediately (on the next line) will probably work ok.
Try this but include UIGestureRecognizerDelegate in your header file.
This is from apple "SimpleGestureRecognizers" example-
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// Disallow recognition of tap gestures in the segmented control.
if ((touch.view == YourButton))
{
//change it to your condition
return NO;
}
return YES;
}

How to go from one gesture recognizer to the another

I have a map drawn with OpenGLES, and I have a pan gesture recognizer that has maximumNumberofTouches set to 1 to pan around the map and a pinch gesture recognizer for zooming. I want to start panning once im done zooming, (one finger is lifted off the screen) but the pan gesture recognizer doesnt kick in until pinchgesturerecognizer is done which is when it detects there are no fingers on the screen. Any ideas?
It may be possible to allow both gestures to be active via the delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// The two recognizers using the delegate (scale and rotate) should both be active.
//
return YES;
}
And keep a BOOL that tracks if the user is zooming, not allowing the code in the pan gesture to execute while that BOOL is YES. Altering the BOOL value by checking the number of touches in the touchesMoved:withEvent method (or perhaps some other UIGestureRegonizer method).
I think this should work, I do something similar in an app that allows scaling, rotating, and dragging, where dragging is only allowed when the user isn't scaling/rotating.
~Good Luck

Resources