• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - UIPageViewController 在 iOS 中自动滚动

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:38:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在制作一个应用程序,其中 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap