Do gesture recognizers override the scrollview's ability to scroll? - iphone

In my app, a top view has pan gesture recognizers to move the view to the left and to the right to reveal two other views underneath it. I'm trying to put a scrollview into the top view, but i can't seem to get it to scroll. I'm using storyboards for the scrollview but the gesture recognizer is implemented programmatically. I have the scroller insets set to exceed the size.
I'm thinking that maybe the gesture recognizers take priority over the scrollview's ability to scroll, but I can't find any way to check.. Unless I'm looking over something stupid, could someone please help with this?

UIScrollView implements a private UISwipeGestureRecognizer that does hold priority over any other gesture. But, it's one many people have overridden (it's fairly easy, just a for-in loop or two) to hook into it's events or provide their own subclass, which is the only way to get other gestures to recognize simultaneously. Actually, that's the name of the delegate method you need to implement, - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

Related

Gesture is not working when used on transparent UIView when subclassed to detect objects behind it

Hi all,
I am having some issue with detecting touches. Please refer image where yellow and brown are my UIView which is subclassed to detect the transparent touches. I have added three gesture recognizers pan, tap and rotation on that UIViews, but when I subclass UIView and override the - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event method to detect all the Imageviews added, gesture recognizers stop working outside the ImageView. And if I am not using subclass then it's unable to detect touches for ImageView on the yellow UIView. I tried the solution from this link
Forwarding UIGesture to views behind
though it is not the same as my requirement, but it didn't help! Any help/hint is appreciated! Thanks in advance.
I have resolved the issue myself. I achieved the my target by applying gesture recognizers on superview not separate for each uiimageview or the colored uiviews. I have added uiviews on superview and now gesture is getting applied to imageview also. And also changed the code in gesture actions so that gesture will get applied to touched imageview and not on superview.
Try implementing gestureRecognizerShould begin in your subclass, test to see if the class of the recognizer is those classes [gestureRecognizer class] == [UITapGestureRecognizer class] for example, and return yes if it is.
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/gestureRecognizerShouldBegin:
The only way I've found to fix this is to add your stack of views as children to a UIView (A). It should be the same size as your transparent view/ all of the child views on your stack. You should implement the gesture recogniser in the view controller if and connect (A) as the iboutlet (just drag and drop a gesture recogniser onto A in interface builder for example).
(View Controller)
|->(A)
|->(1) Need gesture
|->(2) Need another gesture
|->(3) Transparent (messing everything up)
Connect the gesture handlers to the appropriate children of A who need to handle them.
For View Controller
#interface AUIViewController : UIViewController <bla, bla, UIGestureRecognizerDelegate >
For (1)
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer{/*yada yada*/}
In my opinion, this is an apple bug.
My answer is pretty much this but it is a work around for what is in my opinion, a bug.
P.S, sorry I didn't fix this without using interface builder. Hope its still helpful

Zoomable UIScrollView with UIButtons capturing touches

I have a UIScrollView and I'm implementing the viewForZoomingInScrollView: delegate method which returns a UIImage which the user can zoom and pan. I've also got some UIButtons as sub views of the UIScrollView which I'm using as annotations, like Google Maps.
The problem I'm having is that a lot of the UIImage can be obscured by the UIButtons when zoomed right out. When trying to pinch to zoom the UIButtons are receiving the touch event instead and the zoom is not happening. You end up having to carefully place your fingers in clear space to zoom.
I note the Google Maps app seems to work ok when there are lots of annotation views, you can still pinch.
I guess I need to priorities the touches, the UIScrollView needs to respond to pinches and pans, while the buttons just taps.
Anyone have experience of this?
I had this exact issue, and it was kind of weird. It wasn't due to any gesture recognizer on the button, it was the UIScrollView's pinch gesture recognizer that was being forwarded to the UIButton for some reason. Also, the UIButton was responding to certain UIGestureRecognizerDelegate calls (like -gestureRecognizerShouldBegin:) but not others (like -gestureRecognizer:shouldReceiveTouch:). It's like the UIScrollView was selectively forwarding some delegate calls to the UIButton and some not.
I finally found a very easy way to solve this issue:
yourScrollView.pinchGestureRecognizer.delaysTouchesBegan = YES;
yourScrollView.pinchGestureRecognizer.delaysTouchesEnded = YES;
Luckily, the default pinch gesture recognizer on UIScrollView's are publicly accessible, and the two delaysTouches property are NO by default. When you set them to YES, if the gesture recognizer could possibly or does recognize a pinch gesture even when it starts on top of a UIButton, it won't forward those touches to the UIButton, and the UIButtons will no longer interfere with the UIScrollView's zooming.
try this
UIGestureRecognizer* tapRecognizer = nil;
for (UIGestureRecognizer* recognizer in yourButton) {
if ( ![recognizer isKindOfClass:[UITapGestureRecognizer class]] ) {
[yourButton removeGestureRecognizer:recognizer];
break;
}
}
so you remove all the gesture recognizers from the button different from UITapGestureRecognizer
take a look at UIView's hitTest:withEvent: method. Inside that method youll need to check for which view you want to return. The view you return will be the one recieving the touches. for example, you can subclass the button and override that method to return the ImageView for your particular scenario.
I fixed this problem in a different way, but this might not be your case.
My buttons were added on an image view, the image view being added by itself to another container view. In the:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
... method, I returned the image view - bad. Changing to return my very first container view in hierarchy fixed the touches.

iOS: how to allow all gestures/events *with a couple exceptions* to pass through a top level view to its subviews

I have an atypical iOS interface. Perhaps it's not practical but I'm giving it a go. Hope someone can help!
I have a menu in the form of a UIVIew. It contains 5 small UIImageViews. A UIPinchGestureRecognizer is attached to the UIVIew. When pinched inward, the 5 UIImageViews animate from off screen to form a circle in the middle of the window. When pinched outward, they animate back offscreen. Everything works great there.
I'd like to be able to, at any point in the application, pinch the screen to reveal the menu, select one of the 'buttons' (UIImageView), and load the associated subview.
The real problem is, if the current visible view is a UIScrollView or UITableView, my app is having trouble figuring out whether the menu or other subview should handle the touch event. If I really focus and make sure two finger touch the screen at the EXACT same time, the pinch will work and pull the menu inward. But otherwise, it attempts to scroll the current visible view.
I would like all events except the pinch gesture, (and a tap gesture when the menu is visible), to pass through the menu view to the rest of the subviews.
I understand I can override the hitTest:withEvent method to determine the correct view to handle the event, but I'm unclear at this point how exactly to use it. Neither the Apple docs nor any answers I've read on stack overflow have made this method clear to me.
Any help is much appreciated.
As UITableView is a subclass of UIScrollView, it inherits all of UIScrollView's properties including its gesture recognisers.
UIScrollView declares a UIPinchGestureRecognizer and UIPanGestureRecognizer. I'm not sure of the implementation details but I imagine the UITableView disables the pinch gesture recogniser as you are not supposed to be able to zoom a tableview!
In any case, you can attach your own UIPinchGestureRecognizer to the table view:
UIPinchGestureRecognizer *yPGR = [[UIPinchGestureRecognizer alloc]
initWithTarget:probablySelf action:yourMenuShowSelectorHere];
UITableView *tv = ...
// ...
[tv addGestureRecognizer:yPGR];
Then, you can make sure that the UITableView scoll does NOT scroll until your pinch has failed:
[tv.panGestureRecognizer requireGestureRecognizerToFail:yPGR];
This way, the UITableView will not scroll until it is sure that it has not detected a pinch.
EDIT: UIScrollView only uses (or at least declares public access to) UIGestureRecognizers in iOS 5 and up.

UIGestureRecognizer not receiving touches during UIScrollView deceleration

I've been trying to create a UITextView subclass that handles swiping away the keyboard like in the Message.app.
I have a UIPanGestureRecognizer added to the keyWindow of my app, and the gesture delegate is configured for shouldRecognizeSimultaneouslyWithGestureRecognizer. Everything works fine except for when the UIScrollView is decelerating, during that phase it is possible to pan without the touches being registered.
You can take a look at a very simple github sample project here.
I have tried adding the UIPanGestureRecognizer directly to the viewController.view and to the scrollView, same issue occurs. I have also tried setting scrollView.panGestureRecognizer requireGestureRecognizerToFail: with my UITextView subclass gesture recognizer.
Any ideas as to why this may be happening?
Instead of creating a new UIPanGestureRecognizer, maybe you could use the one on the UIScrollView, and add your own pan logic to that gesture recognizer with - (void)addTarget:(id)target action:(SEL)action.

Nested UIScrollViews scrolling simultaneously

I have two nested UIScrollViews: the parent limited to horizontal paging and the child limited to vertical scrolling. The content is one large view that can be freely dragged around, but snaps to one of three horizontal sections. The default behavior of the nested scroll views is to only allow scrolling in one direction at a time, but I wanted to allow simultaneous dragging in both direction to maintain the feeling of manipulating a single large view.
My present solution involved isolating the vertical scroll view's gesture and setting its delegate to my view controller:
for (UIGestureRecognizer *gesture in scrollView.gestureRecognizers)
if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
gesture.delegate = self;
Then, I implemented the delegate method to allow the gestures of the paging view to recognize simultaneously with the pan gesture of the scroll view:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if (gestureRecognizer.view == scrollView && otherGestureRecognizer.view == pageView)
return YES; // allow simultaneous scrolling of pageView and scrollView
return NO;
}
This solution mostly works, but it will occasionally act glitchy when I drag the view around, particularly when I move it around quickly with mouse or drag it around past the view bounds. Specifically, one of the scroll views will temporarily jump back to where it started as if that gesture had been canceled, but then it will jump back if I keep scrolling.
What I want to know is if there is a simpler or more reliable method to achieve scrolling like this that I've overlooked, or if there's anything that I can do to eliminate the glitchy behavior.
The glitchy behavior was happening when the views were dragged out of the content area, released, and then tapped/dragged again before the scroll views bounced back. This could happen, for example, when the view was scrolled by several small swipes. One of the scroll views would get confused and try to decelerate (bounce) while simultaneously being dragged, causing it to jitter back and forth between the origin and where it had been dragged to.
I was able to fix this by reversing the nesting of the scroll views (paging view inside of the vertical scrolling view) and by adding the delegate to the paging view's UIPanGestureRecognizer instead of to the scrolling view's gesture. Now it scrolls naturally as if it was a single scroll view while still conforming to paging only in the horizontal direction. I don't think it was intended for scroll views to be tricked into scrolling simultaneously like this, so I'm not sure if the original glitchy behavior was the result of a bug, or just a consequence of doing something unintended.
Just been struggling with this too and hit a wall with 'NSInvalidArgumentException', reason: 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.' as pointed out by Rythmic Fistman above.
Found a way around it... first subclass the inner UIScrollView and make it as a
Then all you have to do in the implementation is:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Seems to work fine.