简体   繁体   中英

UI Page View Controller Scroll Won't Scroll Backwards

I have implemented a UIPageViewController that I want to be able to scroll infinitely to the right (as in the user swipes to the left) and be able to scroll backwards in the other direction all the way to the start. However I don''t want the user to be able to scroll left (as in the user swipe to the right) on the very first page, even though I only have three pages that loop, how can I do this?

I have already figured out how to make an infinite scroll with three view controllers to the right ( as in user scrolls to the left) but when the user tries to scroll to the left (as in user swipes to the right) it bugs out and the screen goes white?

Can anyone improve on this code so the user can infinitely scroll both ways, not just one? and if possible prevent the user from scrolling backwards on the very first panel? thanks.

PageViewController.h

#import <UIKit/UIKit.h>

@interface PageViewController : UIPageViewController
 <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
@end

PageViewController.m

 #import "PageViewController.h"

@interface PageViewController ()

@end

@implementation PageViewController
{
    NSArray *myViewControllers;


}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    self.dataSource = self;

    UIViewController *p1 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController1"];
    UIViewController *p2 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController2"];
    UIViewController *p3 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"ContentViewController3"];

    myViewControllers = @[p1,p2,p3];

    [self setViewControllers:@[p1]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];


    NSLog(@"loaded!");

}

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    return myViewControllers[index];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
     viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];

    --currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];

    ++currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}


-(NSInteger)presentationCountForPageViewController:
(UIPageViewController *)pageViewController
{
    return myViewControllers.count;
}

-(NSInteger)presentationIndexForPageViewController:
(UIPageViewController *)pageViewController
{
    return 0;
}

@end

You can use Flag to know the initial View (first view).then use this scroll enable and disable method to prevent the swipe.

-(void)setScrollEnabled:(BOOL)enabled forPageViewController:(UIPageViewController*)pageViewController{
for(UIView* view in pageViewController.view.subviews){
    if([view isKindOfClass:[UIScrollView class]]){
        UIScrollView* scrollView=(UIScrollView*)view;
        [scrollView setScrollEnabled:enabled];
        return;
    }
}

Hope this Will Work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM