2015-09-22 161 views
0

我在我的應用程序中有一個UIViewController,導航條如下圖所示。 UIViewController包含一個4頁的UIPageViewController。導航欄上的每個選項卡代表UIPageViewController中的一個頁面。以編程方式平滑滾動UIPageViewController

enter image description here

我想實現這些UIPageViewController頁面之間平滑滾動,即如果我目前是第一個選項卡上,我點擊最後一個選項卡上的導航欄上的UIPageViewController必須通過一切順利滾動標籤到最後一個。雖然現在這個工作,但我看起來不安,因爲看起來很緊張,所以我不滿意。我正在使用

- setViewControllers:direction:animated:completion: api在頁面之間滾動。

這是我的代碼看起來像

- (void)navigateToNextPage { 

    //you have navigated to the destination, return 
    if(self.destinationPageIndex == self.currentPageIndex) 
     return; 

    if(self.destinationPageIndex < 0 || self.destinationPageIndex >= [self.pageViewControllerIdentifiers count]) 
     return; 

    //determine the scroll direction 
    UIPageViewControllerNavigationDirection direction = self.destinationPageIndex < self.currentPageIndex ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward; 

    //get the index of the next page to scroll to 
    NSInteger nextPageIndex = self.destinationPageIndex < self.currentPageIndex ? self.currentPageIndex-1 : self.currentPageIndex+1; 

    //get the storyboard identifier of the page to navigate to 
    NSString *identifier = [self.pageViewControllerIdentifiers objectAtIndex:nextPageIndex]; 
    NSString *storyboardName = @"Sales"; 
    id viewController = [COSCheckoutNavigationViewController instantiateControllerFromStoryboardName:storyboardName storyboardIdentifier:identifier]; 
    NSArray *viewControllers = @[viewController]; 

    __weak typeof (self) weakSelf = self; 
    [self.pageViewController setViewControllers:viewControllers direction:direction animated:YES completion:^(BOOL finished) { 
     [weakSelf updateCurrentPageIndex]; 

     //navigate to the next page after small delay 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
      [weakSelf navigateToNextPage]; 
     }); 
    }]; 
} 

有沒有一種方法,我可以提高在此代碼爲更好的滾動性能?其他任何技術能幫助我實現我想要的嗎?

任何幫助,將不勝感激!

+0

hello @tbag你可以幫我,我堆放在pageViewController中,當pageviewcontroller滾動到下一頁或前頁時,我想改變自定義tabViews的顏色變化。 –

回答

0

斯威夫特3.0.2

我面臨同樣的問題,因爲你,我發現我工作的解決方案。請記住,我使用UISegmentedControl來管理childViewControllers。現在,我不允許滑動手勢,因爲它弄亂了我的觀點。此遞歸調用允許在UIPageViewController內部的每個UIViewController之間的動畫。

// We call this method recursively until we find our desired view controller 
// We do it this way to provide an animated transition if you find yourself in 
// the first view and want to go to the last one. If we just jumped to the 
// desired VC, we will not se any animation between the different VCs in the 
// middle and it would look like there are only two VCs 
func segmentedControl(index: Int) { 
    let desiredVC = pages[index] 
    if pages[currentIndex] != desiredVC { 
     if index > currentIndex { 
      self.setViewControllers([pages[currentIndex+1]], direction: .forward, animated: true, completion: nil) 
      currentIndex += 1 
     } else { 
      self.setViewControllers([pages[currentIndex-1]], direction: .reverse, animated: true, completion: nil) 
      currentIndex -= 1 
     } 
     segmentedControl(index: index) 
    } 
} 

我的UISegmentedControl只是將選定的索引發送到此方法。