UIPageController and gesture recognizers - ios

I am using UIPageController to implement a page based navigation application. I would like to enable the swipe gestures only at the margin of the page and prevent that from the inner content view. The page has a margin of 20 pixels:
contentView.frame = CGRectInset(self.view.frame, 20, 20);

Upon recognising a gesture you should be able to retrieve the locationInView and then if this is an acceptable value proceed, otherwise not.

First add in the class interface.
#interface MyPageViewController :UIViewController<UIPageViewControllerDelegate, UIGestureRecognizerDelegate>
Then in viewDidLoad add
for (UIGestureRecognizer *recognizer in self.pageViewController.gestureRecognizers) {
recognizer.delegate = self;
}
Then implement shouldReceiveTouch method
#pragma mark - UIGestureRecognizer delegate methods
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint touchPoint = [touch locationInView:self.pageViewController.view];
CGRect innerRect = CGRectInset(self.pageViewController.view.frame, 40, 40);
if (CGRectContainsPoint(innerRect, touchPoint)) {
return NO;
}
return YES;
}

Related

Forward gestures to UITableViews

I've an UISplitViewController-Subclass with two UITableViewControllers (master & detail) in my Storyboard.
My BOSplitViewController is extended by a subview to draw in it.
_linienView = [[BODrawLinienView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_linienView];
In BODrawLinienView i've implemented my UIPanGestureRecognizer to Draw UIBezierPaths.
_panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(recognizedGesture:)];
_panRecognizer.minimumNumberOfTouches = 1;
_panRecognizer.maximumNumberOfTouches = 1;
_panRecognizer.delegate = self;
_panRecognizer.cancelsTouchesInView = NO;
[self addGestureRecognizer:_panRecognizer];
And the UIGesturerecognizerDelegate methods are implemented there, too.
#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint velocity = [panGestureRecognizer velocityInView:self];
return fabs(velocity.y) < fabs(velocity.x); // < >
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
The check for velocity in gestureRecognizerShouldBegin: allows me to avoid vertical events handled by the recognizer, who only should accept more or less horizontal events.
But all events avoided by this check aren't forwarded to the UITableViews in the SplitViewControlles ViewControllers.
EDIT: I've prepared a demo-project of this behavior on my Dropbox directory
Does anybody know how to realize forwarding of events to these Views?

Subview implementation of gestureRecognizerShouldBegin: interferes with parent view's implementation of that method

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.

UITableView swipe gesture requires near perfect accuracy

I'm working on a custom swipe event for a UITableView that uses custom UITableViewCell subclass. I included the UIGestureRecognizerDelegate in my header, and have this in viewDidLoad:
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:swipeLeft];
My swipeLeft method looks like so:
-(void)didSwipe:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateEnded)
{
CGPoint swipeLocation = [recognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
NSDictionary *clip = [self.clips objectAtIndex:swipedIndexPath.row];
NSLog(#"Swiped!");
}
}
It's sort of working, but the swipe has to be incredibly precise. Like nearly impossibly precise.
I almost got it working by using a UIPanGestureRecognizer instead, but unfortunately it didn't play nice with the global side drawer component that uses a global pan gesture recognizer (ECSlidingViewController for those interested).
Is there any way around this? Any help would be appreciated, as I've been googling around and browsing SO for hours looking for a solution.
As pointed out by Kolin Krewinkel on Twitter, implementing these 2 delegate methods did the trick:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
I was having a similar problem using the ECSlidingViewController and swipe-to-delete on a UITableView (the top view controller in my case slides to the left to reveal the menu).
I fixed the problem by adding a delegate to the default panGesture property of my ECSlidingViewController like this to only pull in the menu if the swipe starts in the very right-hand edge of the screen:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer locationInView:gestureRecognizer.view].x > ([UIScreen mainScreen].bounds.size.width - 60.0))
{
return YES;
}
return NO;
}

UIPanGestureRecognizer conflict with scrollview

I'm trying to add a pan gesture recognizer to a view containing a scrollview, but I guess I've problems with priorities.
My global UIView has a UIPanGestureRecognizer set like this:
_bottomPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(bottomPanGestureDetected:)];
_bottomPanGestureRecognizer.minimumNumberOfTouches = 2;
_bottomPanGestureRecognizer.maximumNumberOfTouches = 2;
_bottomPanGestureRecognizer.delaysTouchesBegan = NO;
_bottomPanGestureRecognizer.delaysTouchesEnded = NO;
I want to recognize this gesture to display another view from the bottom with some sort of pinch down-to-up.
The problem is that the scrollview is recognizing its own pan gesture before mine.
So I tried to delay it thanks to:
[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:_bottomPanGestureRecognizer];
And it's working, the scrollview event is fired after my two finger down to up recognizer, but the problem is now when I only use one finger to scroll in the scrollview, the scroll works after a small delay.
I would like to have no delay for this event, is this possible? Any idea welcomed!
Cheers.
Cyril
In case it isn't solved yet, i solved the problem for me.
I added a UIPanGestureRecognizer to a UIScrollView to detect two finger pan gestures and the default UIScrollView behaviour (scrolling to something) still workes.
So what i did is to add the UIPanGestureReconizer to the UIScrollView:
UIPanGestureRecognizer *pangestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(displayReloadIndicator:)];
pangestureRecognizer.minimumNumberOfTouches = 2;
pangestureRecognizer.delegate = self;
[self.scrollView addGestureRecognizer:pangestureRecognizer];
[pangestureRecognizer release];
After this i added the code:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
After this, i implemented the pan gesture recognizers action method.
- (void) displayReloadIndicator:(UIPanGestureRecognizer*) panGestureRecognizer {
UIGestureRecognizerState gestureRecognizerState = gestureRecognizer.state;
CGPoint translation = [gestureRecognizer translationInView:self.scv_bibgesamt];
if (gestureRecognizerState == UIGestureRecognizerStateBegan) {
// create a UIView with all the Pull Refresh Headers and add to UIScrollView
// This is really much lines of code, but its simply creating a UIView (later you'll find a myRefreshHeaderView, which is my base view) and add UIElements e.g. UIActivityIndicatorView, a UILabel and a UIImageView on it
// In iOS 6 you will also have the possibility to add a UIRefreshControl to your UIScrollView
}
else if (gestureRecognizerState == UIGestureRecognizerStateEnded
|| gestureRecognizerState == UIGestureRecognizerStateCancelled) {
if (translation.y >= _myRefreshHeaderView.frame.size.height + 12) { // _myRefreshHeaderView is my baseview
//so the UIScrollView has been dragged down with two fingers over a specific point and have been release now, so we can refresh the content on the UIScrollView
[self refreshContent];
//animatly display the refresh view as the top content of the UIScrollView
[self.scrollView setContentOffset:CGPointMake(0, myRefreshHeaderView.frame.size.height) animated:YES];
}
else {
//the UIScrollView has not been dragged over a specific point so don't do anything (just scroll back to origin)
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//remove the view (because it's no longer needed)
[_myRefreshHeaderView removeFromSuperview];
}
}
UPDATE:
In case you may wish to integrate the swipe back functionality from your navigationcontroller, you should integrate following code:
- (void) viewDidLoad {
[super viewDidLoad];
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
//setup view controller
}
and
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (gestureRecognizer == _panGestureRecognizer
&& [self.navigationController.viewControllers count] > 1) {
CGPoint point = [touch locationInView:self.view.window];
if (point.x < 20
|| point.x > self.view.window.frame.size.width - 20) {
return NO;
}
}
return YES;
}
Implement panRecognizer delegate to enable simultaneously recognize UIScrollView UIGestureRecognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (_panRecognizer == gestureRecognizer) {
if ([otherGestureRecognizer.view isKindOfClass:UIScrollView.class]) {
UIScrollView *scrollView = (UIScrollView *)otherGestureRecognizer.view;
if (scrollView.contentOffset.x == 0) {
return YES;
}
}
}
return NO;
}
SWIFT 4
If using a scrollView you can use below, it is only a panGestureRecognizer when a scrollView is dragging at the very top:
translation.y > 0 means you are moving from top to bottom and
locationInScrollView.y < 500.0 means you end dragging at 500 or less (you can customize this) to prevent that the refresh is done in middle or bottom of the scroll.
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
let locationInScrollView = scrollView.panGestureRecognizer.location(in: scrollView)
if translation.y > 0 && locationInScrollView.y < 500.0 {
print("scrollView refresh: Y: \(locationInScrollView.y)")
setupUI()
}
}

UIGestureRecognizer and UITextView

I'm working on implementing a UITextView in which I only want to respond to touches that are in a specified part of the text view.
I have a gesture recognizer attached to the view, and that works fine, until I set the view to become first responder, which I do if the tap's point in the view is greater then an X an Y value.
- (IBAction)textViewTapped:(UIGestureRecognizer *)sender {
CGPoint point = [sender locationOfTouch:0 inView:self.view];
NSLog(#"x ix %f, y is %f", point.x, point.y);
if (point.x > 96 && point.y > 106)
[self.myTextView becomeFirstResponder];
}
The problem is, once it is set to be first responder, and then resigned by tapping outside of that text view, my gesture recognizer method is never called again. If I tap in the area that doesn't set first responder, then my method gets called as many times as I tap. If I set and then resign first responder, it doesn't respond after the first time it is resigned.
- (IBAction)viewTapped:(UIGestureRecognizer *)sender {
[self.view endEditing:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(textViewTapped:)];
[self.myTextView addGestureRecognizer:tap];
NSArray *gestures = [self.myTextView gestureRecognizers];
NSLog(#"got %d recognizers", [gestures count]);
}
In just trying things, if I add a new gesture recognizer after each resign, then that works, but is obviously not a good solution.
Any thoughts?
I have the same problem, and i solved it by implementing dummy UIGestureRecognizerDelegate
add this to your code
myGestuerRecognizer.delegate = self
then implement UIGestureRecognizerDelegate
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES; }
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer{
return YES; }
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES; }
this works for me
iOS 10 Swift 3.0
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

Resources