Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController check when swiped back or forward

I want to keep track of the index using the UIPageViewController. Whenever I swipe I need to index++ or index--. This delegate method gets called whenever you swipe back or further:

- (void)pageViewController:(UIPageViewController *)pvc didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    // If the page did not turn
    if (!completed)
    {
        // You do nothing because whatever page you thought
        // the book was on before the gesture started is still the correct page
        return;
    }

    // I want to check here whenever the page was swiped back or further
}

How do I check in this method if the user swiped back or further? I know there are the 2 DataSource methods "viewControllerAfterViewController" and "viewControllerBeforeViewController" but I cannot check if the page transition has completed (and I can do this in the above method) any idea how I could know if the user swiped back or further in the above method?

like image 876
nonuma Avatar asked Apr 24 '13 20:04

nonuma


2 Answers

use protocol:

MyClass : UIViewController <UIPageViewControllerDataSource,UIPageViewControllerDelegate

declare a atributes:

@property(nonatomic) NSInteger currentIndex;
@property(nonatomic) NSInteger nextIndex;

and in the methods:

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
    NewsTableViewController *controller  = [pendingViewControllers firstObject];
    self.nextIndex = [self.arrViews indexOfObject:controller];
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    if (completed) {
        self.currentIndex = self.nextIndex;
    }
    self.nextIndex = 0;
}

there you will have the current page. Thanks to Corey Floyd in enter link description here

like image 80
jc.vargas.valencia Avatar answered Sep 22 '22 09:09

jc.vargas.valencia


According to the documentation there does not appear to be a way to tell whether the user has swiped the page forward or backward. The boolean 'finished' will tell you whether or not the user has completed the page turn.

A workaround:

Create an int variable and using the viewControllerAfterViewController and viewControllerBeforeViewController methods either increase or decrease the value of the variable. Use that to test whether or not they moved forward or backward.

Edit: You could use presentationIndexForPageViewController from the documentation

Edit 2: Check this link here There is a method named setViewControllers:direction:animated:completion: the direction will be either UIPageViewControllerNavigationDirectionForward or UIPageViewControllerNavigationDirectionReverse

Edit 3: Code - This is assuming you know which view controller will be called by either going forward or backward:

Create a variable on your appDelegate and a setter method:

int indexVar;

- (void)setIndex:(int)indexVar;

Then on your view controllers (forward or backward) either increase the value or decrease the value (viewDidLoad):

(AppDelegate *)[[UIApplication sharedApplication] delegate] setIndex:<whatever>];

Something along those lines. This won't be an exact way to accomplish your goal, but hopefully it will get you headed in the right direction.

like image 23
Jordan Avatar answered Sep 22 '22 09:09

Jordan