I am adding two gesture on a view. One is tap gesture and second is swipe gesture. Whenever user tries to swipe the view , tap gesture method gets called (tap gesture gets detected). What could be the reason ? Tap gesture should get ignore in this case.
below is the code .
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(itemSwiped:)];
swipeGest.direction = UISwipeGestureRecognizerDirectionUp ;
[messageView addGestureRecognizer:swipeGest];
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(itemSelected:)];
[messageView addGestureRecognizer:gest];
use this delegate method to avoid other gesture
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return false;
}else if([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]){
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer];
return false;
}
return true;
}
Because your using multiple gesture on same view. this may over hide one another property .For Ex: first gesture recognizer recognizes the gesture (tap in this case) it cancels all touch events. Therefore the remaining gesture recognizers will never finish recognition, and will never fire their events.
In order to add more than one UIGestureRecognizer onto the same view, you need to set the delegate property (UIGestureRecognizerDelegate)
yourGesture.delegate = self
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (gestureRecognizer isKindOfClass: [UISwipeGestureRecognizer class] || gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]) {
return YES
} else {
return NO
}
}
Try this;
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(itemSwiped:)];
swipeGest.direction = UISwipeGestureRecognizerDirectionUp ;
[messageView addGestureRecognizer:swipeGest];
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(itemSelected:)];
gest.numberOfTapsRequired = 1;
[messageView addGestureRecognizer:gest];
-(void)itemSwiped:(UISwipeGestureRecognizer*)gesture
{
NSLog(#"swipe");
}
-(void)itemSelected:(UITapGestureRecognizer*)gesture
{
NSLog(#"tap");
}
Change single tap to doubletap gesture. so you can be sure user only double tap or swipe. It is good advice from me please follow it.
Related
Swipe to Delete is not working properly in a Tableview. In viewDidLoad I have following code.
for (UIGestureRecognizer *gesture in _addressTblView.gestureRecognizers) {
if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
gesture.cancelsTouchesInView = YES;
gesture.delaysTouchesBegan = YES;
}
and I have implemented UIGestureRecognizer methods in my View Controller.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
// For smooth swipe to delete in UITableView Cell, because Pan Gesture of Side Menu also competes.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
EKLog(#"gesture recognizer 1 %#", gestureRecognizer);
EKLog(#"gesture recognizer 2 %#", otherGestureRecognizer);
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
Whenever swipe to delete is performed one gesture succeeds and other fails.
**gesture recognizer 1** <UIScrollViewPanGestureRecognizer: 0x1022a2ed0; state = Ended; delaysTouchesBegan = YES; delaysTouchesEnded = NO; view = <UITableView 0x10292e800>; target= <(action=handlePan:, target=<UITableView 0x10292e800>)>>
**gesture recognizer 2** <UIScrollViewPanGestureRecognizer: 0x1022a8bc0; state = Failed; cancelsTouchesInView = NO; delaysTouchesEnded = NO; view = <UITableViewWrapperView 0x102965000>; targets= <(
"(action=handlePan:, target=<UITableViewWrapperView 0x102965000>)",
"(action=handleSwipeBeginning:, target=<UITableViewWrapperView 0x102965000>)"
)>>
As seen in debug console, two gesture recognizer are competing for each other. Sometimes swipe to delete works and sometimes it doesnt. Please help. Thanks.
i am creating drawer
self.isShowMenuVC = NO;
_menuView = [MenuViewController viewController];
[self.menuView setDelegate:self];
[self addChildViewController:self.menuView];
[self.menuView.view setFrame:CGRectMake(-kMenuTableWidth, 0, kMenuTableWidth, self.view.frame.size.height)];
[self.view addSubview:self.menuView.view];
[self.menuView didMoveToParentViewController:self];
UITapGestureRecognizer *outsideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(outsideTapped:)];
[self.view addGestureRecognizer:outsideTap];
outsideTap.delegate = self;
and when button tap i just set frame of _menuView.view to behave like a drawer
what i want is to detect touch outside of drawer but i am not able to do it
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view == self.menuView.view) {
NSLog(#"Touch Drawer");
} else {
NSLog(#"Touch Outside");
}
return YES;
}
but it is always show Touch Outside"
i am missing something but don't know what thanks in advance
Also try with 2 gesture but not working because one gesture in self.view so, when i tap in drawer method call 2 times.
for that i tried to disable one gesture, still calling two times
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (gestureRecognizer == self.touchInDrawer) {
NSLog(#"Touch in drawer");
[self.touchOutSideDrawer setEnabled:NO];
} else {
NSLog(#"Outside");
[self hideMenuView];
}
return YES;
}
The UITapGestureRecognizer cannot detect the touch outside the view which it belongs to.
You need to create another UITapGestureRecognizer and add them to self.menuView.view.
Also you can make two #property for your gesture recognizers and check them inside method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (gestureRecognizer == self.firstGesture) {
NSLog(#"Touch in first gesture");
} else {
NSLog(#"Touch in another gesture");
}
I have solve this issue with adding two gesture
#property (strong,nonatomic) UITapGestureRecognizer *touchInDrawer;
#property (strong,nonatomic) UITapGestureRecognizer *touchOutSideDrawer;
as per #Eugene Zaychenko's Answer but there is still problem because delegate method calling two times
also i can't [self.touchOutSideDrawer setEnabled:NO]; when touch in drawer tapped, because after that it will remove from the view and never executed again if [self.touchOutSideDrawer setEnabled:YES];
but most interesting thing is
_touchInDrawer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(outsideTapped:)];
[self.menuView.view addGestureRecognizer:self.touchInDrawer];
self.touchInDrawer.delegate = self;
_touchOutSideDrawer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(outsideTapped:)];
[self.view addGestureRecognizer:self.touchOutSideDrawer];
self.touchOutSideDrawer.delegate = self;
outsideTapped method calling only one time so i shifted my all code there and is working
- (void) outsideTapped:(UITapGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer == self.touchOutSideDrawer) {
// [self.view removeGestureRecognizer:self.touchOutSideDrawer];
[self hideMenuView];
NSLog(#"Outside");
} else {
NSLog(#"Touch in drawer");
// [self.touchOutSideDrawer setEnabled:NO];
}
}
Please help me with the below scenario.
Self.view (Swipe Gesture added here)
UicollectionView Object in subView (Default pan,swipe gestures are there)
Want to disable scrollview scrolling/failCollectionViewPan gesture for 2 finger swipe event
Means collection view will not scroll if 2 finger swipe executed
Another approach can be to disable Collection view scrolling while 2 fingers used. Over here, I want not to swipe collection view on 2 finger swipe.
I have implemented this code, but its slowing down scrolling.
[self.collectionView.panGestureRecognizer shouldBeRequiredToFailByGestureRecognizer:_swipeL];
Then I have implemented below codes
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
Now both, Swipe and Scroll are working together.
Then, I have tried in a below manner, but still not got fixed.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]]){
if(gestureRecognizer.numberOfTouches==2){
if( [[otherGestureRecognizer.view class] isSubclassOfClass:[UITableViewCell class]] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:#"UITableViewCellScrollView"] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:#"UITableViewWrapperView"] || [NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:#"UIScrollViewPanGestureRecognizer"] || [NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:#"UIScrollViewPagingSwipeGestureRecognizer"])
{
NSLog(#"Allow&Disable %#", [otherGestureRecognizer description]);
[gestureRecognizer requireGestureRecognizerToFail:otherGestureRecognizer];
return NO;
}
}
}
return YES;
}
Also I have implemented below 2 methods to fix it but fails.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer NS_AVAILABLE_IOS(7_0);
Its is able to fix for ScrollView with such ways, but the same ways are not working for UICollectionView. Because of, colection view's default pan gesture, it cannot be able to modified. While tring this way, app crashed.
If you want to detect pan gesture, try something like this. But you need to recognise direction of pan gesture:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGesture:)];
panGesture.delegate = self;
[self.yourCollectionView addGestureRecognizer:panGesture];
}
- (void)panGesture:(UIPanGestureRecognizer *)gesture;
{
if (gesture.numberOfTouches == 2) {
NSLog(#"Two fingers pan gesture");
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
&& gestureRecognizer.numberOfTouches == 2) {
return NO;
}
return YES;
}
Your problem with UISwipeGestureRecognizer is, that it fires later than UIPanGesture, so in the shouldRecognizeSimultaneouslyWithGestureRecognizer delegate call, swipe gesture is always otherGestureRecognizer, and UIPanGesture is always gestureRecognizer, and in this function you can only disable otherGestureRecoginzer...
UPDATE:
Another solution: use another UIPanGestureRecognizer to disable scroll pan gesture:
- (void)viewDidLoad
{
[super viewDidLoad];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
swipeGesture.delegate = self;
swipeGesture.numberOfTouchesRequired = 2;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:nil];
panGesture.delegate = self;
[self.tableView addGestureRecognizer:panGesture];
[self.tableView addGestureRecognizer:swipeGesture];
}
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture;
{
NSLog(#"Two fingers swipe gesture");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
&& gestureRecognizer.numberOfTouches == 2) {
return NO;
}
return YES;
}
This answer might be late, but I did have a similar problem today and couldn't find a solution, until I figured something out myself. It's quite simple, actually.
Swift 5
We create a gesture
let gesture = UIPanGestureRecognizer(target: self, action: #selector(functionCall))
gesture.minimumNumberOfTouches = 2
gesture.maximumNumberOfTouches = 2
We then assign that gesture to the collectionView, effectively overwriting the scroll gesture on it.
collectionView.addGestureRecognizer(gesture) // simply overwrites the 2 finger gesture on the collectionView
It now is possible to scroll the collectionView with 1, 3 or more fingers, but the 2 finger pan gesture is blocked.
I have a ParentViewController whose view has a subview. I want ParentViewController to have the initial opportunity to evaluate gestures in its view, and only upon failure of those gestures should the subview then have the chance to evaluate the gesture. But instead things seem to be going the opposite direction, and the subview is actually evaluating a pinch gesture that belongs to the parent view. Here's how things are set up:
ParentViewController has a pinch gesture recognizer whose delegate is set to ParentViewController:
pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchRecognizer.delegate = self;
[self.view addGestureRecognizer:pinchRecognizer];
ParentViewController implements the UIGestureRecognizerDelegate protocol, specifically implementing the method gestureRecognizerShouldBegin:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer //DEPENDENCY
{
return [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]];
}
ParentViewController's view has a subview. The subview has a tap gesture recognizer and a pan recognizer:
_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(stepTapped:)];
[self addGestureRecognizer:_tapRecognizer];
_tapRecognizer.numberOfTapsRequired = 1;
_tapRecognizer.delegate = self;
_panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[self addGestureRecognizer:_panRecognizer];
_panRecognizer.maximumNumberOfTouches = 1;
_panRecognizer.delegate = self;
The subview also implements the UIGestureRecognizerDelegate protocol, specifically implementing the method gestureRecognizerShouldBegin:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:[self superview]];
return (fabsf(translation.x) > fabsf(translation.y) && translation.x < 0);
} else if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
return NO;
}
Finally, ParentViewController tells the subview's gesture recognizers to only run when its' own pinch gesture fails:
[subview.panRecognizer requireGestureRecognizerToFail:pinchRecognizer]; //GESTURE DEPENDENCY
[subview.tapRecognizer requireGestureRecognizerToFail:pinchRecognizer]; //GESTURE DEPENDENCY
Despite these last two lines of code, I find that the subview's gestureRecognizerShouldBegin: method gets called before that of ParentViewController. Furthermore, the implementation of gestureRecognizerShouldBegin: in ParentViewController won't even run unless I modify the subview's implementation to return YES if a pinch gesture is taking place (see final line):
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:[self superview]];
return (fabsf(translation.x) > fabsf(translation.y) && translation.x < 0);
} else if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
return [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]];
}
With the above code, the subview's gestureRecognizerShouldBegin: runs and, upon returning YES at the end if it's a pinch gesture, ParentViewController's gestureRecognizerShouldBegin: gets called.
In case it isn't clear, the behavior I expect is that the ParentViewController's gestureRecognizerShouldBegin: always gets called first, and only upon failure will the subviews' implementation get called.
I have a cutsom UITableViewCell implemention.
I have registered this subclass of UITableViewCell for a UIPanGestureRecognizer which i use to swiping the cells to the right or left.
// in the UITableViewCell subclass :
UIGestureRecognizer* recognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:
self
action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
recognizer.cancelsTouchesInView = NO;
Now I want to to present a view controller when the user does a two finger swipe "up"
on the screen.
So, I added a UISwipeGestureRecognizer to the tableview.
// code in the view controller containing the tableview reference.
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionUp];
[swipe setDelaysTouchesBegan:NO];
[[self tableView ]addGestureRecognizer:swipe];
swipe.cancelsTouchesInView= YES;
[swipe setNumberOfTouchesRequired:2];
swipe.delegate = self;
self.tableView.multipleTouchEnabled = YES;
But when I do a two finger swipe on the screen, the pan gesture gets triggered .
How can I solve this ?
As sooper's says, setting the maximumNumberOfTouches = 1 will probably work.
For others trying to deal with 2 gestureRecognizers at the same time that are both 1 touch gestures I found that making sure to set this delegate to yes
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
and then in the gesture recognizer action you can check for a certain translation or whatever you need and cancel one of the gesture recognizers.
Such as:
- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.superview];
if(panRecognizer.state == UIGestureRecognizerStateBegan)
{
if(fabsf(translation.x) < fabsf(translation.y))
{
//deactivate horizontal gesture recognizer
panRecognizer.enabled = NO;
panRecognizer.enabled = YES;
}
else //if(fabsf(translation.x) > fabsf(translation.y))
{
//deactivate vertical gesture recognizer
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
}
//other statements like stateChanged and stateBegan
}