36
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIPageViewControllerで無限にループさせたい

Posted at

UIPageViewControllerDataSourceでViewControllerを返すときに、先頭と末尾をくっつければよし。
XCodeのサンプルコードのModelController.mを↓とする。

ModelController.m

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
    if( index == NSNotFound){
        return nil;
    }
    if (index == 0) {
        // 先頭に行ってたら最後のControllerを返す(=ループ)
        index = [self.pageData count]-1;
    }else{
        index--;
    }
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    
    index++;
    if (index == [self.pageData count]) {
        // 末尾に行ってたら先頭のControllerを返す(=ループ)
        index = 0;
    }
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
36
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?