UISwipegesture not working with UITableview - iphone

UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(resignTextView)];
swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
swipeGestureRecognizer.numberOfTouchesRequired=1;
[self.tableview addGestureRecognizer:swipeGestureRecognizer];
-(void)resignTextView
{
[textView resignFirstResponder];
}
I do not know Why resignTextView method not get called?

The tableview has a scrollview and you can get the delegates of it to get the scroll events.
Check the docs for the UIScrollViewDelegate protocol, and implement the -scrollViewDidScroll: or -scrollViewWillBeginDragging: methods as appropriate to your situation.
Just implement the methods and it works since the tableview delegates implements the underlying scroll delegates.

You are adding Swipe Gesture on Table View and in Downword direction. As you know Table View already has a Scroll and it scrolls upward and downward direction. If you add a Swipe Gesture in Downward direction. Then every time when you swipe in downward direction calls scroll methods of scroll and table moves scroll and device layer detect no swipe action, so ur method not called. In a very few situation when sometime there is less cell and table is not scrolling then try to check it again ur swipe method get called. Both swipe in downward direction and scrolling are not works properly in simultaneously.

you missed : after resignTextView

Related

iPhone - Stop UISwipeGestureRecognizer from triggering UIPanGestureRecognizer

I have found many posts on managing gestureRecognizers, but I have not been able to find my answer, or rather get anything to work correctly.
I have a UISwipeGestureRecognizer set to down in my Main ViewController, and a PanGestureRecognizer on the x axis attached to an overlay view that pulls the overlay view over to overlay my main view from the right.
Swipe moves down, Pan is for Left to Right.
I want the swipe down to work on all the views, and it does, but when the user swipes down on my overlay view, it also triggers the UIPanGestureRecognizer, which hides the view.
I think this is because there is slight movement in the X direction when you swipe, so the Pan is getting triggered.
How do I get my UIPanGestureRecognizer to only fire if it is a definite Pan to the left or right, and not the trailing end of a swipe?
I have tried shouldRecognizeSimultaneouslyWithGestureRecognizer but it can't really work correctly. Currently they are all set to delegate self.
Any help would tremendously appreciated. Thank you!
The solution was placing
[panGesture requireGestureRecognizerToFail:swipeGesture];
in my viewController's viewDidLoad.
and implementing
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
in my ViewController.m.
I had it placed in my overlay view's viewDidLoad and .m, and that was never getting called. Silly me.

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.

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

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

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.

How To Make Scroll View Scroll Even If A Button Subview Is Pressed

There are countless questions about UIScrollViews, touch events and subviews, but I couldn't find one that solved my dilemma. I have a UIScrollView with delayed and cancellable content touches in which I have added UIButtons. I can scroll the view wherever I touch (including the buttons), but if my finger lingers on a button for too long, it registers as touching the button, as I want and is expected. However, if I then move my finger as if trying to scroll, the scroll view does not scroll - how can I make it do so? The basic effect I am trying to achieve is like a UITableView handles touches for the cells.
Thanks for your help.
Try implementing the following in your UIScrollView subclass:
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}