2014-10-31 70 views
1

我正在製作一個應用程序,其中PageViewController應該在某個時間間隔內滾動。目前我已完成手動滾動。用戶可以滾動任何圖像,它會工作。但堅持實現用戶觸摸自動滾動。以下是我的代碼。請幫助我。UIPageViewController在iOS中自動滾動

- (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; 
} 

回答

3

@Daniel你的鏈接查看documentation壞了,你的「答案」做一點解釋你解決問題的方法。對於那些有興趣閱讀UIPageViewController Class的文檔。

要注意這裏的方法是:

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

的UIPageViewController希望你告訴它提供什麼樣的數據,並使用基於值什麼控制器。這裏我告訴我的UIPageController子類是委託和數據訪問。配置委託方法和viewControllerAtIndex數據源方法後,我添加了一個自定義時間間隔,它調用了我的loadNextController方法。在該方法中,我正在檢查「下一個」控制器是否爲零。如果它不是零,我將「下一個」控制器作爲@ [啓動控制器]傳遞給第二次調用setViewControllers。如果它是零,我只是將索引值重置爲零,並使用我的「下一個」控制器作爲@ [起始控制器]調用setViewControllers。這個自定義子類用於自動尋呼到下一個或第一個控制器。獎勵是用戶交互仍然由UIPageViewController Class處理。

注意:初始調用setViewControllers是必需的,因爲您的UIPageViewContrller不知道如果可以首先加載任何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 
            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]; 
} 

注意:這是行不通的;但是,當內部手勢識別器更改控制器時,我並不總是返回正確的控制器。一路上我正在失去對指數的追蹤。我會用我的完成實施更新這個。

+0

請更新回答Swift 3 – Mamta 2017-06-19 16:03:11

0

也許你正在尋找的方法是setViewControllers:direction:animated:completion:

當然,你需要提供你想在動畫的最後看到的視圖控制器。

您可以通過動畫集調用此爲YES,控制器將自動頁面:)

更多

0

工作@ Ryan的回答。修正@Ryan問題「,但是當內部手勢識別器改變控制器時,我並不總是返回正確的控制器。在這個過程中,我正在失去對索引的跟蹤。

只編輯了幾行,它爲我工作。如果有人需要相同的話,可以考慮分享。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
_pageTitles = @[@"Tips n Tricks", @"Join to learn", @"Share to get points"]; 
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"]; 
self.pageViewController.dataSource = self; 
self.pageViewController.delegate = self; 

[NSTimer scheduledTimerWithTimeInterval:10.0 
           target:self 
           selector:@selector(loadNextController) 
           userInfo:nil 
           repeats:YES]; 
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 = _containerView.frame; 
self.pageControl.numberOfPages = _pageTitles.count; 
self.pageControl.currentPage = 0; 
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; 
[self addChildViewController:_pageViewController]; 
[self.view addSubview:_pageViewController.view]; 
[self.pageViewController didMoveToParentViewController:self]; 
[self.view bringSubviewToFront:self.pageControl]; 

} 

實現數據源PageViewController

的方法
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController 
{ 
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex; 
    if ((index == 0) || (index == NSNotFound)) { 
     index = _pageTitles.count; 
    } 
    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]) { 
     index = 0; 
    } 
    return [self viewControllerAtIndex:index]; 
} 
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index 
{ 
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) { 
     return nil; 
    } 
    PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"]; 
    pageContentViewController.titleText = self.pageTitles[index]; 
    pageContentViewController.pageIndex = index; 
    return pageContentViewController; 
} 
的PageController委託

實施didFinishAnimating方法去獲得PageViewController的當前索引和更新的PageControl相應

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed 
{ 
    PageContentViewController *currentViewController = pageViewController.viewControllers[0]; 
    currentIndex = (int)currentViewController.pageIndex; 
    [self.pageControl setCurrentPage:currentIndex]; 
} 

,並在最後執行的方法在動畫上更新PageViewController

- (void)loadNextController { 
    currentIndex++; 
    PageContentViewController *nextViewController = [self viewControllerAtIndex:currentIndex]; 
    if (nextViewController == nil) { 
     currentIndex = 0; 
     nextViewController = [self viewControllerAtIndex:currentIndex]; 
    } 
    [self.pageViewController setViewControllers:@[nextViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 
    [self.pageControl setCurrentPage:currentIndex]; 
} 

當前索引被全局聲明以跟蹤當前PageViewController的索引並相應地更新PageController。

希望它有幫助..快樂編碼.. :)

相關問題