UIPageViewController automatic scrolling in iOS

Kashif

I am making one app, in which PageViewController should scroll in some time interval. Currently I have done till manual scrolling. User can scroll any images and it will work. But stuck with achieve automatic scrolling with user touches. Below is my code. Please help me on this.

- (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:@"PageViewController"];
    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 *)viewControllerAtIndex:(NSUInteger)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:@"PageContentViewController"];
    pageContentViewController.imageFile = self.pageImages[index];
    pageContentViewController.titleText = self.pageTitles[index];
    pageContentViewController.pageIndex = index;

    return pageContentViewController;
}

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

    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

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

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

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

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

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

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

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

@Daniel your link is broken and your "answer" does little to explain your solution to the question. For those interested in reading documentation for the UIPageViewController Class.

The method to pay attention to here is :

- (void)setViewControllers:(NSArray *)viewControllers
                 direction:(UIPageViewControllerNavigationDirection)direction
                  animated:(BOOL)animated
                completion:(void (^)(BOOL finished))completion

The UIPageViewController expects you to tell it what data to deliver and what controller to use based on a value. Here I am telling my UIPageController Subclass to be the delegate and dataSouce. After configuring the delegate methods and the viewControllerAtIndex dataSource method, I added a custom interval which calls my loadNextController method. In that method I am checking to see if the "next" controller is nil. If it is not nil I pass the "next" controller as the @[starting controller] back to a second call to setViewControllers. If it is nil I just reset the index value to ZERO and call setViewControllers with my "next" controller as the @[starting controller]. This custom subclass serves to page to the next or first controller automatically. The bonus is that user interaction is still handled by the UIPageViewController Class.

Note: The initial call to setViewControllers is required because your UIPageViewContrller will not know what if any viewController can be loaded first. My second call to setViewControllers does nothing more than skip to the next or first viewController.

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

    /*! setup an interval */
    [NSTimer scheduledTimerWithTimeInterval:10.0
                                     target:self
                                   selector:@selector(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];
} 

Note: This does work; however I am not always returning the correct controller when the internal gesture recognizer changes the controller. Somewhere along the way I am losing track of the index. I will update this with my finished implementation.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Automatic scrolling on load to show all of a page

분류에서Dev

WPF ListBox automatic scrolling start and stop behavior

분류에서Dev

UIPageViewController - Detect scrolling halfway into the next view controller (almost working) to change button color?

분류에서Dev

Sublime Text 2, disable Build output panel/Console automatic scrolling

분류에서Dev

UITableView not scrolling while adding on google mapView iOS

분류에서Dev

iOS-유추 된 UIPageViewController 크기 대 자유형

분류에서Dev

UIPageViewController 내부의 iOS UItextview가 스크롤되지 않음

분류에서Dev

iOS의 UIPageViewController에서 데이터를 전달하는 방법

분류에서Dev

iOS Swift-UISegmentedControl을 사용하여 UIPageViewController 페이지 변경

분류에서Dev

Objective C iOS에서 UIPageViewController 배경이 투명하지 않음

분류에서Dev

UIPageViewController and setViewControllers:

분류에서Dev

Recreating Chrome iOS scrolling navigation bar effect in jquery/javascript

분류에서Dev

"Fixed" header and footer with scrolling middle in Web-app on iOS 7.1.2

분류에서Dev

iOS UITableView scroll position jumps when scrolling up

분류에서Dev

UIPageViewController의 뷰 프레임은 iOS에서 예측할 수 없습니다.

분류에서Dev

Hiding the current page of a UIPageViewController

분류에서Dev

UIPageViewController의 버튼

분류에서Dev

UIPageViewController with multiple views

분류에서Dev

UIPageViewController 및 setViewControllers :

분류에서Dev

UIPageViewController 구현

분류에서Dev

UIPageViewController 빈 화면

분류에서Dev

UIPageViewController 내의 AlertViewController

분류에서Dev

Contrainer View의 UIPageViewController

분류에서Dev

iOS-다른 XIB 파일에서 여러 ViewController를 사용하여 UIPageViewController를 만드는 방법

분류에서Dev

iOS-다른 XIB 파일에서 여러 ViewController를 사용하여 UIPageViewController를 만드는 방법

분류에서Dev

iOS : UIPageViewController에서 오른쪽 스 와이프 제스처가 응답하지 않음

분류에서Dev

Scrolling BG missing after coming back from being suspended (iOS-SpriteKit-iphone)

분류에서Dev

iOS 8 self-sizing cells - wrongly calculated height for labels and Infinite scrolling

분류에서Dev

UIPageViewController transitioning to another view on event

Related 관련 기사

  1. 1

    Automatic scrolling on load to show all of a page

  2. 2

    WPF ListBox automatic scrolling start and stop behavior

  3. 3

    UIPageViewController - Detect scrolling halfway into the next view controller (almost working) to change button color?

  4. 4

    Sublime Text 2, disable Build output panel/Console automatic scrolling

  5. 5

    UITableView not scrolling while adding on google mapView iOS

  6. 6

    iOS-유추 된 UIPageViewController 크기 대 자유형

  7. 7

    UIPageViewController 내부의 iOS UItextview가 스크롤되지 않음

  8. 8

    iOS의 UIPageViewController에서 데이터를 전달하는 방법

  9. 9

    iOS Swift-UISegmentedControl을 사용하여 UIPageViewController 페이지 변경

  10. 10

    Objective C iOS에서 UIPageViewController 배경이 투명하지 않음

  11. 11

    UIPageViewController and setViewControllers:

  12. 12

    Recreating Chrome iOS scrolling navigation bar effect in jquery/javascript

  13. 13

    "Fixed" header and footer with scrolling middle in Web-app on iOS 7.1.2

  14. 14

    iOS UITableView scroll position jumps when scrolling up

  15. 15

    UIPageViewController의 뷰 프레임은 iOS에서 예측할 수 없습니다.

  16. 16

    Hiding the current page of a UIPageViewController

  17. 17

    UIPageViewController의 버튼

  18. 18

    UIPageViewController with multiple views

  19. 19

    UIPageViewController 및 setViewControllers :

  20. 20

    UIPageViewController 구현

  21. 21

    UIPageViewController 빈 화면

  22. 22

    UIPageViewController 내의 AlertViewController

  23. 23

    Contrainer View의 UIPageViewController

  24. 24

    iOS-다른 XIB 파일에서 여러 ViewController를 사용하여 UIPageViewController를 만드는 방법

  25. 25

    iOS-다른 XIB 파일에서 여러 ViewController를 사용하여 UIPageViewController를 만드는 방법

  26. 26

    iOS : UIPageViewController에서 오른쪽 스 와이프 제스처가 응답하지 않음

  27. 27

    Scrolling BG missing after coming back from being suspended (iOS-SpriteKit-iphone)

  28. 28

    iOS 8 self-sizing cells - wrongly calculated height for labels and Infinite scrolling

  29. 29

    UIPageViewController transitioning to another view on event

뜨겁다태그

보관