iOS working with multiple swipe recognizers - ios

I have right and left swipe recognizers on my view, as well as a table.There is a problem when user swipes left->right on the table view, table handle them and swipes table content instead of execution of left->right swipe recognizer methods. How do I increase the sensitivity of left->right swipe recognizers?
Here's the code for the recognizers:
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight)];
swipeRecognizer.numberOfTouchesRequired = 1;
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
UISwipeGestureRecognizer *swipeRecognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft)];
swipeRecognizer1.direction = UISwipeGestureRecognizerDirectionLeft;
swipeRecognizer1.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:swipeRecognizer1];

You can require one gestureRecognizer to fail before another one is invoked.
[lowerPriorityGestureRecognizer requireGestureRecognizerToFail:higherPriorityGestureRecognizer];

It would help greatly if you let us know whether the view is in/over/under the table rather than saying you have both a view and a table. That matters, alot.

Related

SWRevealViewController panGestureRecognizer with UIScreenEdgeGestureRecognizer

I've been looking for solution for this question,but didnt find any one. So I use SWRevealViewController for sidebar menu in my project but I want that side menu would open when user beging to touch from left edge of the view. I want this because I have another swipeGestureRecognizer for left and right swipe.For adding left and right swipes I'm use this code:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipedToRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: #selector(swipedToLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
So my question is how can I make that side menu would open only when touches begins from left edge?
UPD 1
I use this code to open side menu by panning at any part of the screen
[self.view addGestureRecognizer: self.revealViewController.panGestureRecognizer];
And I have recognizer to swipe to right. The problem is once it open menu and once executes swipe's action. I want to allow to open menu only when touches is from left edge
UPD 2 Solved
I solved it by setting draggable border width:
self.revealViewController.draggableBorderWidth = self.view.frame.size.width / 2;
And now gesture from edge to center opens menu, and from center to right border executes swipe's action
What I would do is modify the already existent pop gesture that is built into the UINavigationController class.
You can add a UIPanGestureRecognizer as you mentioned before or add a subView in the top of your view and add a gesture to it:
UIView *topView = ...
[self.view addSubview: topView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(didPan:)];
[topView addGestureRecognizer:pan];
What I would do is modify the already existent pop gesture that is built into the UINavigationController class.
Example code to add to your viewWillAppear:
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
// Your code here
}

disable swipe gesture for iCarousel

I am using iCarousel to display an array of images and I want to disable the swipe gesture. I did not find that in the documentation. not sure if this is doable or not
If you want to disable the swipe gesture then I think are you want to do something like programatically change the image.
For very simply disable the user interaction of carousel.
If you using storyboard then simple remove checkmark of User Inreaction Enabled
If you use by code then following code to disable the User Inreaction Enabled
yourcarousel.userInteractionEnabled = FALSE;
May this help lot to solve your problem.
#Junchao GU If you are Using
https://github.com/nicklockwood/iCarousel
They are using Tap gesture and pan gesture
You have to Comment
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didPan:)];
panGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:panGesture];
//add tap gesture recogniser
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTap:)];
tapGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[_contentView addGestureRecognizer:tapGesture];
in iCarousel.m File
I hope this will help you
It's bad idea to change source code of iCarousel. I think it's better to do next:
carouselView.contentView.gestureRecognizers?.removeAll()
Hope it helps to someone

iOS how to detect swipe left or right on UICollectionView that otherwise only scrolls vertically?

My collection view is working great. It shows a grid of photos and lists hundreds of them. You can swipe vertically to scroll through them all. Life is good. However, I now have a new requirement. I need to be able to detect when the user is swiping left or right. I need to be able to intercept this gesture so I can attach behavior to left and right swipes while keeping intact my collection view's vertical scroll capabilities. Any ideas?
You need two recognizers, one for swiping left, and the other for swiping right:
UISwipeGestureRecognizer* swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeftFrom:)];
swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
and for the handler:
- (void)handleSwipeLeftFrom:(UIGestureRecognizer*)recognizer {
}
Finally, you add it to your view:
[view addGestureRecognizer:swipeUpGestureRecognizer];
The same for the other direction (just change all the Lefts to Rights).
add UISwipeGestureRecognizer to the cells settings its direction as below,
UISwipeGestureRecognizer *swipeRightDir = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipeRightDirection:)];
swipeRightDir.delegate = self;
swipeRightDir.numberOfTouchesRequired = 1;
[swipeRightDir setDirection:UISwipeGestureRecognizerDirectionRight];

WebView and UISwipeGestureRecognizer

I am trying to have a UISwipeGestureRecognizer that is attached to a webview. This works, but I need to also be able to scroll up and down the web view.
Here is my code:
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:upSwipeGesture];
[webView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:upSwipeGesture];
If I comment out the last line, then the webview scrolls but the gesture is not recognized. If I do not comment out the last line, then the gesture is recognized but the webview does not scroll.
I would like to be able to scroll and have the gesture be recognized. Any ideas? Thanks!
UIWebView has its own private views, which also has gesture recognizers attached. Hence, precedence rules keep any gesture recognizers added to a UIWebView from working properly.
One option is to implement the UIGestureRecognizerDelegate protocol and implement the method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer. Return YES from this method for other tap gestures.
This way you'll get your tap handler called, and the web view will still get its called.
Try this,
UISwipeGestureRecognizer *upSwipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(tapAction)];
upSwipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGesture.cancelsTouchesInView = NO;
upSwipeGesture.delegate=self;
[self.view addGestureRecognizer:upSwipeGesture];
ind implement this delegate method,
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

How can I except a part from a UIView for a gesture recognizer?

I am developing an app and I want to scroll through different UIViewControllers with a UISwipeGestureRecognizer.
Everything works, but there is one thing where I couldn't find a solution for.
I add the UIGestureRecognizers like this
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
}
Now I want to know if it is possible to except a part of the self.view where it is possible to swipe to a different view.
I want to do this around a UISwitch.
Can someone please help me?
Best regards,
Lukas Goes
You can use another UIView and add it to self.view. Those UISwipeGestureRecognizer won't work inside that UIView.
Add another UISwipeGestureRecognizer to the switch, and, on your original swipe gesture recognisers, call requireGestureRecognizerToFail: on the switch's new recognizer.
Hope that makes sense, it's such a verbose class... :\
I suggest you to use the delegate method of a gesture recognizer : gestureRecognizer:shouldReceiveTouch:.
In the touch parameter, you have many information, like the position in the screen. You can use this position and test if it is above the switch frame. Return yes or no depending of if you want to trigger the selector associated to the gesture recognizer or not.

Resources