简体   繁体   English

UI页面视图控制器滚动不会向后滚动

[英]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. 我实现了一个UIPageViewController,我希望它可以向右无限滚动(就像用户向左滑动一样),并且可以一直向另一方向向后滚动。 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 PageViewController.h

#import <UIKit/UIKit.h>

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

PageViewController.m 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. 您可以使用Flag来了解初始视图(第一个视图),然后使用此滚动启用和禁用方法来防止滑动。

-(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. 希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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