iOS press and hold gesture + tap - iphone

I have a press and hold gesture. During the the press and hold, I'd like to detect single taps elsewhere on the screen. The problem is that when I start tapping elsewhere on the screen the press and hold gesture is interrupted and does not call the "touch up" function. Is there a way to retain the press and hold while tapping elsewhere?

UIKit provides several mechanisms to make multiple UIGestureRecognizers
on the same UIView work side by side. Which ones and how exactly depends
on your needs and configuration.
One is - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
However for your case you'll have to adopt UIGestureRecognizerDelegate protocol in your view.
Then you should implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method.
For example:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Don't forget to make your UIView a delegate of it's getureRecognizers.
Some references:
UIGestureRecognizer Class Reference
UIGestureRecognizerDelegate Protocol Reference
Event Handling Guide for iOS

Related

How to combine short dragging and long press

I have a UILongPressGestureRecognizer with minimumPressDuration to 0.5. I need it for fast dragging. Now I also want to detect when a user makes a long press without moving the fingers for more than few seconds. How to do it?
Take a look at UIGestureRecognizerDelegate Protocol Reference.
Your UIView will have to adopt this protocol and implement - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer method. If you have only this two recognizers attached you can simply return YES.
Don't forget to set your UIView as a delegate of both UIGestureRecognizers.

Can TouchBegan work together with gestureRecognizer in same view

I try to implement touchBegan and SwipeGestureRecognizer in the same view but it did not
Detect the swipeGestureRecognizer every touch is call touchBegan so, can it works together?
I am not positive about the SwipeGestureRecognizer but I was able to implement the TapGestureRecognizer along with touchesBegan. I would suggest first commenting out the touchesBegan and seeing if the Swipe is recognized. This would be your starting point. It could be that the swipeGestureRecognizer is not connected to the view or that you did not set setUserInteraction: to YES or TRUE. But once you get the swipe to register. Then uncomment your touchesBegan, and test again. There is also this delegate method that may give you more information:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
Also you can check out: UIGestureRecognizerDelegate_Protocol
and UISwipeGestureRecognizer Class Reference

UIGestureRecognizer not consuming taps

I have a UIViewController and I am adding subviews to the main view. The main view has a UIGestureRecogniser and each subview also has a UIGestureRecognizer. In interface builder I have checked canceled in view in the attributes inspector so I would expect taps on the subviews to only fire my gestureRecognizerShouldBegin in the subview handler but they are also firing the handler attached to the view they are place on.
Does anyone have any idea why this might be the case?
I guess I could keep a handle to the particular instance of the GestureRecogniser attached to each subview and compare if the event being fired has come from the correct recogniser but this does not seem like the correct solution. Any help would be much appricated.
Edit: This second solution doesn't even work seems like every UIGestureRecogniser conforming to the current gesture is being fired!
Edit:
The best solution I have come up with is,
(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if([view isEqual:[touch view]] && self.editing)
return YES;
return NO;
}
I would like the Gesture not to receive the touch at all though

Detect if touch on a UIView

I have a viewController that has a UIView as a subview, I was wondering how i might tell if and only if the user has clicked on the UIView from the Viewcontroller.
Is this possible?
Thanks!
Check out the UITapGestureRecognizer documentation:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITapGestureRecognizer_Class/Reference/Reference.html
You'll instantiate a UITapGestureRecognizer in your UIViewController, using initWithTarget:action (passing a selector which will handle your tap). Then add the UIGestureRecognizer to your UIView via the addGestureRecognizer method.
You could use the touchesBegan, touchesMoved and touchesEnded methods. Depending on the application, you could as well use touchesCancelled.
If none of these work, you might want to use UIGestureRecognizers - UIPanGestureRecognizer and UITapGestureRecognizer.
Just in case it might be helpful, you can also use two gesture recognizers at the same time using the method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

iPhone: dismiss keyboard when user touches background

I have read some other articles like here and here but unfortunately there are some differences in my code that won't make these work (so please don't suggest these answers).
I would like to dismiss the keyboard when the user taps the background. Normally this would be easy, except that my UITextField objects are inside a UIScrollView which makes it so I can't catch the touch events (the UIScrollView swallows them so they don't reach the base view). One way to get around this is to register for a generic gesture (a tap), but this catches all taps, including the ones intended for the submit button.
So basically, 'touchesBegan:withEvent:' wont work because it never gets called, and gestures wont work because they dont account for button presses.
Here's the question: is there some way to detect a simple tap on a UIScrollView? Once I detect the tap I know how to do the rest. Thanks!
You can't use touchesBegan:withEvent on the superview of the scrollview, but what about subclassing UIScrollView and handling the touch there? You can then proceed normally with a call to super's implementation to keep from stepping on the UIScrollView's toes:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Insert code to dismiss keyboard if needed */
// This makes sure scrolling proceeds normally.
[super touchesBegain:touches withEvent:event];
}
I also ran into same problem. While in IOS 6.0, it was working fine, but as soon i switched to IOS 5.0, it started to show the same behaviour you have mentioned.
Workout for IOS 5.0 - You should use the UITapGestureRecognizer. Then set its delegate to self and implement the following delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
as
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ! ([touch.view isKindOfClass:[UIControl class]]);
}
#end
above code will verify that the object under touch is not a UIButton or any control element, then only handle the touch
I hope it would solve your problem.