Page-based application and gesture recognition

I created a page-based app in Xcode 4 for iOS5 iPad.

When I launch the application, I see the pages in the book and I can turn them back and forth by tapping the screen or moving my finger from left to right or from right to left.

My problem is that no matter where I click on the screen, it rotates within the borders of the page.

I was able to undo a finger click using this code:

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) { if ([gR isKindOfClass:[UIPanGestureRecognizer class]]) { [[gR view] removeGestureRecognizer:gR]; } } 

How can I identify a specific area on the screen that, when I click on it, and only she, the page will turn?

I ask about this because I put the toolbar at the bottom of the screen, and when I click the button on the toolbar, the page flips. I want to put 2 arrows on the screen, so that only when I click on them, the page will turn over.

Sorry if my explanation is a little rusty. Thanks to everyone.

+4
source share
2 answers

You can connect to the gesture system and determine in which area to take touches.

In this explanation, I assume that your RootViewController has a UIPageViewController as a child of VC:

- Install your root controller to implement UIGestureRecognizerDelegate

-Turn all gesture recognizers for your pageVC into RootViewControllers ViewDidLoad :

 for (UIGestureRecognizer *gR in self.pageVC.gestureRecognizers) { gR.delegate = self; } 

-Finally embed the gesture recognizer in your RootViewController and determine in which zones you want to ignore:

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { CGPoint point = [touch locationInView:self.view]; //Examine point and return NO, if gesture should be ignored. } return YES; } 

Hope this helps

+2
source

Cipramill's answer is correct - here are more details.

IOS documentation suggests adding new views to outline areas in which you want page gestures to become active, but this method is much simpler. Adding code to the default template installed by Xcode 4 in MQ1RootViewController.h and MQ1RootViewController.m:

Change the interface line in MQ1RootViewController.h:

 @interface MQ1RootViewController : UIViewController <UIPageViewControllerDelegate, UIGestureRecognizerDelegate> 

Add this code to the very bottom of the DidLoad window in MQ1RootViewController.m:

 for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) { gR.delegate = self; } 

Add this method to MQ1RootViewController.m:

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] || [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { CGPoint point = [touch locationInView:self.view]; if(point.x < 100 || point.x > 924) return YES; } return NO; } 

Note that the swipe gesture actually comes from the β€œpan” gestures using the page view controller object.

The above restricts gestures at the left and right edges of the screen. This allows you to use gestures to interact with objects in the center of the screen, not accidentally changing the page with an erroneous hit.

+4
source

Source: https://habr.com/ru/post/1393190/


All Articles