stefanceriu / SCPageViewController

Just like UIPageViewController but better.. :)
MIT License
352 stars 70 forks source link

SCSlidingPageLayouter inversed #40

Closed noordawod closed 7 years ago

noordawod commented 7 years ago

Hi there @stefanceriu

Wonderful work here, truly inspiring. Thanks for that!

To my issue:

I only have 2 pages in the pager. And page number 1 (2nd one) is actually the current one. The user is expected to swipe the page to the right (so finger touching and swiping from left to right) to reveal page number 0 (1st one).

I would like to use SCSlidingPageLayouter() with normal zIndex values -- 0 -> 0, 1 -> 1, etc.

However, when the app loads, I see 2nd page fine, but it's unable to swipe to the right. It just doesn't move...

Anything I'm doing wrong?

stefanceriu commented 7 years ago

Hey, thank you for your kind words.

I'm not entirely sure what's wrong on your side but if you take this patch and apply it on top of the demo project you get the effect you're looking for. As you can see there's nothing particularly interesting in there, just assigning the sliding layout unanimated and then navigating to page index 1 unanimated.

Perhaps your datasource is out of sync or the layouter gets unassigned. If you can, send me a small sample project where I can debug this.

noordawod commented 7 years ago

Well, yeah, I know that part :) that's what I originally did, of course.

However, even though page (1) is displayed first (easy), swiping to the right makes page (0) reveal itself rather than sliding OUT page (1) and revealing a non-moving, resting, page (0).

Do you understand or should I add some images?

stefanceriu commented 7 years ago

Oh, okay, I think I get it now. There's this method on the SCPageLayouterProtocol called

- (NSUInteger)zPositionForPageAtIndex:(NSUInteger)index
                   pageViewController:(SCPageViewController *)pageViewController;

you can use to reverse the default zPositions.

You will also need to modify the layout code in currentFrameForPageAtIndex a bit. Here's a working example

- (CGRect)currentFrameForPageAtIndex:(NSUInteger)index
                       contentOffset:(CGPoint)contentOffset
                          finalFrame:(CGRect)finalFrame
                  pageViewController:(SCPageViewController *)pageViewController
{
    if(index == pageViewController.numberOfPages - 1) {
        return finalFrame;
    }

    finalFrame = [self finalFrameForPageAtIndex:index+1 pageViewController:pageViewController];

    finalFrame.origin.x = MAX(finalFrame.origin.x - finalFrame.size.width, MIN(CGRectGetMaxX(finalFrame) - CGRectGetWidth(finalFrame), contentOffset.x));

    return finalFrame;
}

- (NSUInteger)zPositionForPageAtIndex:(NSUInteger)index pageViewController:(SCPageViewController *)pageViewController
{
    return index;
}
noordawod commented 7 years ago

Brilliant, thank you! :)

stefanceriu commented 7 years ago

Happy to help ;)