Pregunta

I have created a page-based application in Xcode 4, for iPad iOS5.

When I run the app, I can see the pages in the book and can flip them back and forward, by tap on the screen or by moving the finger from left to right, or right to left.

My problem is that no matter where I'm pressing in the screen, in the borders, the page turns.

I had managed to cancel the flip with fingers with this code:

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

How can I define a specific area in the screen that when I tap on it, and only it, the page will turn?

I ask this because I put toolbar in the bottom of the screen and when I click on a button in the toolbar the page flips. I want to put 2 arrows on the screen that only when I press on them the page will flip.

Sorry if my explanation is a little bit rusty. Thank you all.

¿Fue útil?

Solución

you could hook into the the gesture system and define which area to accept touches for.

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

-Set your root view controller to implement UIGestureRecognizerDelegate

-Take over all gesture recognizers for your pageVC in your RootViewControllers ViewDidLoad:

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

-Finally implement the gesture recognizer in your RootViewController and define 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

Otros consejos

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

The IOS documentation suggests adding new Views to delineate the areas where you wish the page turning gestures to be active, but this method is much simpler. Adding code to the default template set up by Xcode 4 in MQ1RootViewController.h and MQ1RootViewController.m:

Change interface line in MQ1RootViewController.h:

@interface MQ1RootViewController : UIViewController <UIPageViewControllerDelegate,       
    UIGestureRecognizerDelegate>

Add this code to the very bottom of viewDidLoad 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 is actually derived from a "pan" gesture by the page view controller object.

The above limits the gestures to the left and right edges of the screen. This allows gestures to be used to interact with objects in the center of the screen without accidentally changing the page with an errant swipe.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top