Difficulty activating 3 touch gesture recognizer in iOS app - iphone

I have an app that uses gesture recognizers quite a bit. From the studying I have done, I have found that there is the touchesBegan method of recognizing a gesture, and then there are gesture recognizers, which should be more slick.
The problem I am running into is that the gesture recognizers aren't nearly as responsive or accurate as the touchesBegan method, but are a lot easier to implement, which is obviously why I am using them. If I want to have a 3 finger gesture detected with a gesture recognizer, it is quite difficult because I have to press down my 3 fingers at the EXACT same time, or else it won't fire. This is in contrast to the touchesBegan method that just knows how many fingers you have down at any point.
Am I missing something with the implementation of this seemingly nice gesture feature that is making it not very responsive? I have set the max and min touches to 3, is that incorrect?
Please help. Thanks!!

The reason you need to press at the exact same time is because, by default, only one gesture recognizer can be recognized at a time. So once you press one finger down that recognizer automatically blocks the other two.
Try implementing the UIGestureRecognizerDelegate and using:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
I'm not sure if this will solve the issue or not but it might.
Cheers.

Related

Three Layers of Pan Gesture Recogniser Confusion

Whilst developing an app I have come up against a problem with having too many pan gesture recognisers.
My first pan gesture recogniser is on the MainViewController which is a parent of the RecipeSearchVC. This gesture recogniser slides the whole view left or right.
My second pan gesture recogniser in in the RecipeSearchParametersVC which is a parent of a Page View Controller.
The third pan gesture gesture recogniser is added to a UIControl Wheel nested inside of a view controller which is represented by the PageViewController.
I know this sounds insane and it could be argued that this is poor design. However, I believe that is this worked cohesively it would be fine.
When trying to rotate the wheel it will rotate for a second or two before the gesture is overtaken by either the PageViewController or the MainViewController. More often than not it is the MainViewController that takes over. What techniques could I employ to clearly separate each of these gesture recognisers?
EDIT:
Apologies for the vagueness of my description when it comes to the pan gesture recognisers.
The MainViewController has it's own UIPanGestureRecpgniser to allow it to move everything left or right.
The RecipeSearchParametersVC only has a UIPanGestureRecogniser because of the UIPageViewController it contains. It does not add the gesture recogniser itself, but simply takes them from the the pageViewController.
The UIControl's gesture recognisers allows it to track the rotation it should undergo.
In taking the advice given, I may remove the gestures from the page view controller and substitue them with buttons. I only intended this to work like the images (which can be scrolled to reveal more images) found in iBooks, and so I thought that it would work fine.
UIControl UIPanGestureRecogniser Code
/**
* sent to the control when a touch related to the given event enters the control’s bounds
*
* #param touch uitouch object that represents a touch on the receiving control during tracking
* #param event event object encapsulating the information specific to the user event
*/
- (BOOL)beginTrackingWithTouch:(UITouch *)touch
withEvent:(UIEvent *)event
{
[super beginTrackingWithTouch:touch withEvent:event];
CGPoint touchPoint = [touch locationInView:self];
// filter out touchs too close to centre of wheel
CGFloat magnitudeFromCentre = [self calculateDistanceFromCentre:touchPoint];
if (magnitudeFromCentre < 40) return NO;
// calculate distance from centre
CGFloat deltaX = touchPoint.x - _container.center.x;
CGFloat deltaY = touchPoint.y - _container.center.y;
// calculate the arctangent of the opposite (y axis) over the adjacent (x axis) to get the angle
_deltaAngle = atan2(deltaY, deltaX);
_startTransform = _container.transform;
// selection in limbo so set all sector image's to minimum value by changing current one
[self getSectorByValue:_currentSector].alpha = kMinimumAlpha;
return YES;
}
Unfortunately due to the nature of my controller hierarchy I was forced to rethink the design of my app.
The MainViewController with the UIPanGestureRecogniser has stayed as is.
The UIPageViewController with the UIControl has moved to a separate static view controller.
This works far better but is not yet ideal. The UIPageViewController steals any horizontal panning, however this can probably be fixed by implementing buttons as an alternative to the scrolling.
The UIControl did not have a gesture recogniser, but I override the beginTrackingWithTouch: and other methods to track the touches.
I suppose the answer should be: if you are layering too many gestures, you're doing it wrong.
You will need to add a container to the wheel, and then you can do something like that, if I am not missing something, this code must work.
UIPanGestureRecognizer* pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan1:)];
[self.view addGestureRecognizer:pan1];
UIPanGestureRecognizer* pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan2:)];
[view2 addGestureRecognizer:pan2];
UIPanGestureRecognizer* pan3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan3:)];
[view3 addGestureRecognizer:pan3];
- (void) pan1:(UIPanGestureRecognizer*)sender{
NSLog(#"%#",sender);
}
- (void) pan2:(UIPanGestureRecognizer*)sender{
NSLog(#"%#",sender);
}
- (void) pan3:(UIPanGestureRecognizer*)sender{
NSLog(#"%#",sender);
}
I believe you would have defined 3 different UIPanGesture objects and attached it to the appropriate views.
While technically this is correct it could cause some confusion, for example, if you're having several overlapping views (you have 3 here) and need a touch to be sent to a view that's not at the top of the view stack what would happen? The gesture could end up being confusing.
Instead, it's possible to attach a single gesture recognizer to the superview of several target views and delegate the gesture to the correct view based on the coordinates of where the user is touching. For that you need to normalize the touch coordinates originating from any of the subviews to the superview where the UIPanGesture is defined. That way you can know where the pan happend on the wheel or elsewhere.
PS: This being said, I strongly feel this is a design gotcha and it will hurt you. I have done things like this but eventually you would stumble on some corner case where the user interaction would be horrid. If this is the main view of your app I suggest you rethink the design.
What techniques could I employ to clearly separate each of these gesture recognisers?
You should look into the UIGestureRecognizerDelegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.
This method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer would block the other gesture recognizer from recognizing its gesture. Note that returning YES is guaranteed to allow simultaneous recognition; returning NO, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return YES.
I believe this will solve your problem of gestures being 'overtaken' by other gestures.

How to prioritize gesture recognizers and touches in a UIView

From reading the UIGestureRecognizer Class Reference it is implied that the API will handle the prioritizing of touches and gesture controls for you, making sure that your touchesBegan and related methods are not called on the view unless the gesture recognizers have first failed:
A window delivers touch events to a gesture recognizer before it delivers them to the hit-tested view attached to the gesture recognizer. Generally, if a gesture recognizer analyzes the stream of touches in a multi-touch sequence and does not recognize its gesture, the view receives the full complement of touches. If a gesture recognizer recognizes its gesture, the remaining touches for the view are cancelled.
I have added a swipe gesture to my view, and it is working. Via some logging, when I do a single swipe, the method reports as such. However, my touchesBegan method is also reporting via its log, despite that the touchesCancelled method is, as expected, also receiving a message.
I want, and expect, the gesture recognize to prevent touchesBegan or touchesMoved from being called.
So my question is: for the gesture recognizer to in fact delay touches based on its state, is there additional setup necessary? The docs do not suggest anything else as necessary.
My setup is simply:
swipeUpTwoFinger=[[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doubleSwipeUp:)]autorelease];
swipeUpTwoFinger.direction=UISwipeGestureRecognizerDirectionUp;
swipeUpTwoFinger.numberOfTouchesRequired=2;
[self addGestureRecognizer:swipeUpTwoFinger];
I have also tried this test to make sure a recognizer has failed before processing with touchesBegan (this test should not be necessary if you believe what the docs say above) but the touchesBegan is still processing the log line after this test:
if (swipeUpTwoFinger.state==UIGestureRecognizerStateFailed)
It sounds like you need:
swipeUpTwoFinger.delaysTouchesBegan = YES;

How to set the maximum duration between two taps in UITapGesture?

In my iOS app, I am using UITapGesture with numberOfTapsRequired equal to two. But I need to specify the maximum duration required between two taps.
If the duration taken between two taps is greater that the specified (Say 0.5 sec) the gesture should not work.
Please guide me how can I achieve this.
Thanks in advance!
It seems like you dont need to handle the tap gesture by the way of maximum duration for each tap in your gesture. You just need to specify how many touches and taps required and in the method you can check the state of the tap gesture.
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
// handling code
}
}
Above piece of code is from the apple documentation.
This question reminds me of how the Double Tap gesture was implemented before Apple went out and implemented it themselves in UITapGestureRecognizer.
Before all of that, we used the methods, touchesBegan and touchesEnd to keep track of the number of fingers touching the screen and also, add a delay to make sure we track double taps. Thats when we could use the time which you asked. Now, there is simply no need as R.A pointed out.

Detecting a finger being held on an object

I am trying to have an image that when the user touches it, it wiggles and as soon as the user lifts their finger it stops.
Is there a gesture that I can use to detect when the finger is down, not just on the initial touch, or when the user moves there finger?
I have tried a LongPress gesture, but that does not get called the entire time the finger is on the view. Can anyone help me with the best way to active this. Right now i am doing it using touchesBegin, touchesMoved, touchesEnd, but i was wondering if there is a better way.
Any suggestions are greatly appreciated.
Thanks
EDIT
Based on the comments, I slightly misunderstood the original question, so I edit my answer to a different solution, which hopefully is a bit more clear (and answers the actual question - not the one that was in my head).
A LongPress gesture is continuous (where a tap gesture is not). That means, the recognizer callback will continue to be invoked until the gesture is complete - which does not happen until the "longpress" is released. So, the following should do what you want. NOTE: I think you want to "start shaking" a view when the long-press is recognized, then "stop shaking" the view when the fingers are released. I just pretended you have functions for that. Substitute appropriately.
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
StartShakingView(gestureRecognizer.view);
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
StopShakingView(gestureRecognizer.view);
}
}
The Apple Touches sample includes code that demonstrate using both UIResponder and UIGestureRecognizer methods.
Either should work for what you're doing.
Simple answer - you could make the image a UIButton, and start the wiggle on TouchDown, and stop it on TouchUpInside or TouchUpOutside
It sounds like you want to subclass UIGestureRecognizer, which, as I recall, gets the touchesBegan:... and associated methods. Read the notes on subclassing in the UIGestureRecognizer reference. Or use a UIButton as SomaMan suggests.

iphone sdk how to catch a long press after 1 second?

I'm using UILongPressGestureRecognizer to catch a Long Press event. The problem is I can only respond to it after user release his finger.
How do I implement a function that will respond after user hold for x seconds?
UILongPressGestureRecognizer works for this, check your code again and set duration for long press as -
[longPressGesture setMinimumPressDuration:<#(CFTimeInterval)#>];
Instead of using a gesture recognizer, use the UIView instance methods touchesBegan:withEvent:, touchesMoved:withEvent: and touchesEnded:withEvent:. While they're not as convenient as using a gesture recognizer, you have complete control of the touch interpretation.
Note that if you every try to use these to interpret multitouch gestures, the touch screen is "bouncy", in that the reported number of touches will vary during the touch event processing. My app interprets pinches, which are reported to me as a random sequence of one and two finger touches. I managed to debounce it, but getting the code right was a real PITA.