OStack程序员社区-中国程序员成长平台

标题: ios - UIPageViewController 在 iOS 中自动滚动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:38
标题: ios - UIPageViewController 在 iOS 中自动滚动

我正在制作一个应用程序,其中 PageViewController 应该在某个时间间隔内滚动。目前我已经完成了手动滚动。用户可以滚动任何图像,它会起作用。但坚持通过用户触摸实现自动滚动。下面是我的代码。请帮我解决这个问题。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _pageTitles = @[@"", @"", @"", @""];
    _pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier"ageViewController"];
    self.pageViewController.dataSource = self;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    // Change the size of page view controller
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50);

    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
}

- (PageContentViewController *)viewControllerAtIndexNSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier"ageContentViewController"];
    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.titleText = self.pageTitles[index];
    pageContentViewController.pageIndex = index;

    return pageContentViewController;
}

-(void)viewWillAppearBOOL)animated {
    [super viewWillAppear:YES];

    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerBeforeViewControllerUIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerAfterViewControllerUIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.pageTitles count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

- (NSInteger)presentationCountForPageViewControllerUIPageViewController *)pageViewController
{
    return [self.pageTitles count];
}

- (NSInteger)presentationIndexForPageViewControllerUIPageViewController *)pageViewController
{
    return 0;
}



Best Answer-推荐答案


@Daniel 您的链接已损坏,您的“答案”几乎无法解释您对问题的解决方案。对于那些有兴趣阅读 UIPageViewController Class 文档的人.

这里要注意的方法是:

- (void)setViewControllersNSArray *)viewControllers
                 directionUIPageViewControllerNavigationDirection)direction
                  animated:(BOOL)animated
                completion:(void (^)(BOOL finished))completion

UIPageViewController 希望您根据值告诉它要传送什么数据以及使用什么 Controller 。在这里,我告诉我的 UIPageController 子类是委托(delegate)和 dataSouce。配置委托(delegate)方法和 viewControllerAtIndex dataSource 方法后,我添加了一个调用我的 loadNextController 方法的自定义间隔。在那种方法中,我正在检查“下一个” Controller 是否为零。如果它不是零,我将“下一个” Controller 作为@[starting controller] 传递回对 setViewControllers 的第二次调用。如果它是零,我只是将索引值重置为零,并使用我的“下一个” Controller 调用 setViewControllers 作为@[起始 Controller ]。此自定义子类用于自动分页到下一个或第一个 Controller 。好处是用户交互仍然由 UIPageViewController 类处理。

注意:需要对 setViewControllers 进行初始调用,因为您的 UIPageViewController 将不知道是否可以先加载任何 viewController。我第二次调用 setViewControllers 只是跳到下一个或第一个 viewController。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 0;
    self.delegate = self;
    self.dataSource = self;

    /*! setup an interval */
    [NSTimer scheduledTimerWithTimeInterval:10.0
                                     target:self
                                   selectorselector(loadNextController)
                                   userInfo:nil
                                    repeats:YES];

    /*! set starting controller */
    NSArray *startingViewControllers = @[[self  viewControllerAtIndex:self.index]];

    [self setViewControllers: startingViewControllers
                   direction: UIPageViewControllerNavigationDirectionForward
                    animated: YES
                  completion: nil];
}

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
    if (index > 0 ) {
        return [self viewControllerAtIndex:index-1];
    }
    return nil;
}

- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
    if (index+1 < [self.items count] ) {
        return [self viewControllerAtIndex:index+1];
    }
    return nil;
}

- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index{
    if (index < [self.items count]) {
        PageContentViewController *pageContentViewController =   [[PageContentViewController alloc]init];
        pageContentViewController.pageIndex = index;
        pageContentViewController.image = (UIImage*)self.items[index];
        return pageContentViewController;
    }
    return nil;
}

/*! set next controller */
- (void)loadNextController {
    PageContentViewController *nextViewController = [self viewControllerAtIndex:self.index++];
    if (nextViewController == nil) {
        self.index = 0;
        nextViewController = [self viewControllerAtIndex:self.index];
    }
    [self setViewControllers[nextViewController]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
} 

注意:这确实有效;但是,当内部手势识别器更改 Controller 时,我并不总是返回正确的 Controller 。一路上的某个地方我失去了对索引的跟踪。我将用我完成的实现更新它。

关于ios - UIPageViewController 在 iOS 中自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668536/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4