2

I am using the iBook like page turn transition (UIPageViewControllerTransitionStylePageCurl) to navigate a couple of UIViewController pages.

Therefore I implemented UIPageViewControllerDataSource delegate functions

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{

and

- (UIViewController *)pageViewController:
(UIPageViewController *)pageViewController viewControllerBeforeViewController:
(UIViewController *)viewController

These get called correctly when doing the page turn by gesture. So far, so good.

In addition to this default behavior I'd like to add a long press gesture recognizer to both sides of the page as shortcut to move to the very last page or the very first page quickly. I already managed to add the gesture recognizer to the pages but I don't know how make the page move without the built-in swipe.

How can I trigger the page turn to a specific page (UIViewController) programmatically?

Bernd
  • 11,133
  • 11
  • 65
  • 98

3 Answers3

3

When the user performs the long swipe you can send the desired viewController (first or last page in your case) to the viewControllers argument of the method:

- (void) setViewControllers:(NSArray*)viewControllers
                  direction:(UIPageViewControllerNavigationDirection)direction
                   animated:(BOOL)animated
                 completion:(void (^)(BOOL finished))completion;

for more details see Graham's answer at:

Is it possible to Turn page programmatically in UIPageViewController?

If I read you right, your UIViewControllers that are being displayed contain buttons and you'd like them to also be able to call this function.

To call the method programatically from WITHIN the UIViewControllers being displayed, you could make the UIPageViewController a delegate of your UIViewControllers that are being displayed, then call a delegate method when the buttons are pressed.

A bad way to do it would be to send the UIViewControllers a back-pointer (stored as a WEAK property!) to your UIPageViewController and call respondsToSelector: on it.

Community
  • 1
  • 1
Patrick Borkowicz
  • 1,206
  • 10
  • 19
  • Thanks so much. The approach with the delegate was exactly the missing piece I was looking for. In addition the following tutorial helped to get started with it http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/#comment-190. Yay! :) – Bernd Jun 07 '12 at 11:28
  • Can you give me more details. I want to send data to another view controller and change the view of page controller on pressing a button. – Dinesh Jan 14 '14 at 21:35
0

Have a look at this UIPageViewController method:

- (void)setViewControllers:(NSArray *)viewControllers 
                 direction:(UIPageViewControllerNavigationDirection)direction
                  animated:(BOOL)animated 
                completion:(void (^)(BOOL finished))completion;

UIPageViewController Class Reference

Pfitz
  • 7,336
  • 4
  • 38
  • 51
  • 2
    Thanks for this, however I am not sure where to call this function. I already use the "setViewControllers:direction:animated:completion" function in my PageAppViewController (of class UIPageViewController) which initializes the Array with the pages and acts as data source and delegate. However the buttons that should trigger this function now are part of the UIViewController classes that are being displayed themselves. How can I set the reference from "down there" up to the UIPageView class to call this function? I'd appreciate any additional notes. – Bernd May 29 '12 at 20:27
0

Make sure you did not set self.pageViewController.datasource to self. Then you can use this UIPageViewController function along with gesture recognizers.

   -[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
  1. You can set the delegate of a gesturerecognizer and implement this:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    
  2. Or you can just add a gesture recognizer like UISwipeGestureRecognizer

Community
  • 1
  • 1
KarenAnne
  • 2,764
  • 1
  • 28
  • 21