2013-12-18 16 views
8

NSUrlConnectionデータプルでページビューを動的に読み込んで無限にスクロールできるUIPageViewControllerを設定しようとしています。私は3つのビューから始まります:prev、curr、next; pageData配列の最初または最後のビューに達したときに追加のビューをロードします。無限スクロールUIPageViewController

問題は、追加のビューをviewControllerBeforeViewControllerまたはviewControllerAfterViewControllerにロードすることです。これらのメソッドは、ページ間で1回のスワイプを行うときに2-3回呼び出すことができます。ハーフスワイプ中に呼び出すこともできます。ハーフスワイプは、ページ遷移を決して終了しません。これは、複数のプリロードが途中で終了することを意味します。次に何とかpageViewControllerは現在の場所を忘れてしまいます。

[self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:NO completion:nil]; 

この行はpageViewControllerにおけるデータの追加を通知するために、及びEH適切なビューに移動するために以下で使用されます。私がそれを使用し始めると、スワイプで前/次のビューに行くのではなく、一見無作為なビューにジャンプし始めます。私は逆方向にスワイプする際に前進を開始したら...

は、ページ遷移が完了した後に一度だけ呼ばれる予圧を行うには、より適切な場所はありますか?そして、上の行は呼び出されなければならないのですか、それとも確実に正しいページに移動するためのより信頼できる方法ですか?これをワイヤリングしようと思っていたのを感じました。

// the init line, somewhere early on. uses Scoll style 
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil]; 

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { 
    NSUInteger index = [self.pageData indexOfObject:viewController]; 
    if(index == 1){ 
     // if loading last one on start, preload one more in advance 
     [self loadSecondaryCalData:-1 endView:[[self viewControllerAtIndex:index-1] retain]]; 
    } else if ((index == 0) || (index == NSNotFound)) { 
     return nil; 
    } 

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

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { 

    NSUInteger index = [self.pageData indexOfObject:viewController]; 
    if(index == [self.pageData count]-2){ 
     [self loadSecondaryCalData:1 endView:[[self viewControllerAtIndex:index+1] retain]]; // if last one, preload one more in advance 
    } else if (index == NSNotFound) { 
     return nil; 
    } 

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


- (void)loadSecondaryCalData:(int)howMany endView:(CalendarViewController*)endView { 
    // initiate NSURLConnection async request for view data 
} 

- (void)loadSecondaryCals: (NSDictionary*)data { 
    // callback for async request 

    // ... process view, cal 

    // add new object to page data, at start or end 
    if(next){ 
     [self.pageData addObject:cal]; 
     gotoIdx = [self.pageData count]-2; 
     direction = UIPageViewControllerNavigationDirectionForward; 
    } else { 
     [self.pageData insertObject:cal atIndex:0]; 
     gotoIdx = 1; 
     direction = UIPageViewControllerNavigationDirectionReverse; 
    } 

    [self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:NO completion:nil]; 

// alternate method to above line 
/* 
__block CalendarPageViewViewController *blocksafeSelf = self; 
__block int blocksafeGotoIdx = gotoIdx; 
__block UIPageViewControllerNavigationDirection blocksafeDirection = direction; 
[self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:YES completion:^(BOOL finished){ 
    if(finished) 
    { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [blocksafeSelf.pageViewController setViewControllers:@[[blocksafeSelf.pageData objectAtIndex:blocksafeGotoIdx]] direction:blocksafeDirection animated:NO completion:nil]; 
     }); 
    } 
}]; 
*/ 


} 

UPDATE:以下の高速応答へ

おかげで、私は知らなかった偉大な機能を参照します。ここでは、完全に遷移の完了時に一度だけ呼び出されるプリロードメソッドがあります。これは、信頼性の低い飛び跳ねた景色や場所を忘れてしまったようです。ありがとう@sha!

// function to detect if we've reached the first or last item in views 
// detects for full completion only 
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { 

    if(completed == TRUE && [previousViewControllers count] > 0 && [pageData count] > 1){ 
     CalendarViewController *firstCal = [pageData objectAtIndex:0]; 
     CalendarViewController *lastCal = [pageData objectAtIndex:([pageData count]-1)]; 

     CalendarViewController *prevCal = [previousViewControllers objectAtIndex:0]; 
     CalendarViewController *currCal = [[pageViewController viewControllers] objectAtIndex:0]; 

     NSLog(@"transition completed: %@ -> %@", prevCal.week_day_date, currCal.week_day_date); 
     if(currCal == firstCal){ 
      // preload on begining 
      [self loadSecondaryCalData:-1 endView:firstCal]; 
     } else if(currCal == lastCal){ 
      // preload to end 
      [self loadSecondaryCalData:1 endView:lastCal]; 
     } 
    } 
} 

答えて

5

あなたはローディングコールをpageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:ハンドラー内で行う必要があると思います。このメソッドは、アニメーションが終了し、新しいViewControllerがアクティブになったときに呼び出されます。

+0

ありがとう、これは素晴らしい!このソリューションの上記の更新を参照してください。この機能でとてもクリーンです。 – Miro

+0

スウィフト3:pageViewController(:UIPageViewController、didFinishAnimating終了:ブール、previousViewControllers:[のUIViewController]、完了transitionCompleted:ブール_ pageViewController)FUNC –