I setup a UITapGestureRecognizer on a UIScrollView in the storyboard. The Scroll View contains other contents (two UIView, one UIWebView).
The Gesture Recognizer properties are as follows:
Action: dismissPopover
delegate: postViewController
gestureRecognizers: Scroll View
state: enabled
numberOfTapsRequired: 1
numberOfTouchesRequired: 1
cancelTouchesInView: YES
delayTouchesBegan: NO
delayTouchesEnded: YES
The Scroll View (relevant) properties are as follows:
userInteractionEnabled: YES
canCancelContentTouches: YES
However, when I tap anywhere on the Scroll View, the gesture does not work.
The delegate class (conforming to UIGestureRecognizerDelegate) must implement
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This way, the tap gesture will work.
The scroll view has it's own gesture recognizer.
You'll need to override that gesture recognizer or disable that first.
Then and then only, your gesture recognizer will work.
The better way to do this is to use the tap gesture inside the scroll view rather than adding your gesture recognizer.
Related
When I tap on a UIButton, a UIView MyView appear from the bottom a cover a third of the screen. I would like that when I tap somewhere outside this view, it disappears.
I thought about adding another transparent UIView right under MyView and add a tab gesture on it with the dismiss function but I'm sure there is something cleaner than this.
So I thought about adding the tap gesture MyTapGesture to dismiss MyView on self.view of the UIViewController. The problem is that outside this view, I have other UIControls and gestures that capture also any touch at the same time than MyTapGesture.
How can I make MyTapGesture the priority gesture outside MyView and ignore all other gesture, taps, etc...?
You may have to use the gesture delegate methods to handle two tapGestureRecognizer activate the one you need depending on scenario
#pragma mark - UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([tapGestureRecognizer1 isEqual:gestureRecognizer]) {
return [tapGestureRecognizer2 isEqual:otherGestureRecognizer];
}
if ([tapGestureRecognizer2 isEqual:gestureRecognizer]) {
return [tapGestureRecognizer1 isEqual:otherGestureRecognizer];
}
return NO;
}
I have a table view with a pan gesture on it. When I start panning the tableview up and down the tableView selection gets disable and I can't select tableview cells.
UIPanGesture* tablePanGesture =[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(gestureHandler:)];
tablePanGesture.delegate = self;
[tableView addGestureRecognizer:tablePanGesture];
[tablePanGesture setCancelsTouchesInView:NO];
And I using the following delegate to let my gestures and tableview gestures work together:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
I have implemented the method for table view didSelectRowAtIndexPath but when I use panning it is not called.
Is there any conflict between pan gesture and tableview Delegate?
You are missing this line:-
[tablePanGesture setCancelsTouchesInView:NO];
This will let the UIPanGesture recognize the pan gesture and also pass the touch to the next responder means your table view select or tap.
I'm having trouble adding a long press gesture to my UITableView. Well, technically, I do have a long press gesture recognizer, but I set the minimum taps duration to 0.08. I did this because I want to have the same general action for tapping and holding down a cell, but the action only changes based on how long the cell was held. Anyway, here is the code where I add the gesture recognizer (in viewDidLoad) :
var longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
longPress.minimumPressDuration = 0.08
longPress.delegate = self
longPress.cancelsTouchesInView = false
self.tableView.addGestureRecognizer(longPress)
self.tableView.panGestureRecognizer.requireGestureRecognizerToFail(longPress)
In my handleLongPress() function, I get the CGPoint where there was a long press and then get the tableView cell from that.
So basically, if I scroll quickly, (like if I flick the screen), the table view scrolls fine. If I try to scroll slowly, the long press event fires and I can't scroll.
All I want to do is to be able to scroll slowly, I want the tableviews default scroll gesture to override any long press.
Thanks!
ScrollViews have a panGestureRecognizer property, you can call requireGestureRecognizerToFail on your long press recognizer with the scrollView's panGestureRecognizer as an argument and it will only fire if the pan gesture in the scroll view fails.
This category might solve your problem:
#interface UITableView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
#end
#implementation UITableView (CellSwipeAdditions)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
#end
i'm using ECSlidingViewController and i've a problem when using pan Gesture to open the sliding menu the tableview in the main view still have the scrolling gesture
i need to remove the gesture of the main view and add it back when it slide back
in the InitViewController add the following method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return NO;
}
if return YES it will make the undesirable behavior
I have two subviews view1 and view2. I have added LongPress and Pan gesture to my parentview.
When I longPress on view1, I will present a draggableview with popup animation just below the fingure and will continue dragging dragView to view2.
In this process panGesture selector is not called but Longpress gesture selector is called.
After i remove the fingure from dragview and then start dragging again then the panGesture selector is called.
What I need is, once the dragview is created, disable(not permanently but until pan gesture state ended is called) the longpress gesture and the pan gesture selector should be called
You have a delegate method called:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
Take a look at it, you need to return YES.