我試圖設置一個UIPageViewController
,它可以通過NSUrlConnection
數據拉動動態加載頁面瀏覽量來無限滾動。我從3個視角開始:prev,curr,next;並在達到pageData
陣列中的第一個或最後一個視圖時加載附加視圖。無限滾動UIPageViewController
問題是我在viewControllerBeforeViewController
或viewControllerAfterViewController
中加載了額外的視圖。看起來這些方法在頁面之間進行一次滑動時可以調用2-3次。它們也可以在半滑動過程中調用,從不完成頁面轉換。這可能意味着多個預加載開始過早完成。然後以某種方式pageViewController
忘記當前的位置。
[self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:NO completion:nil];
這條線以下用於用信號在pageViewController
加法數據,並且去誒相應的視圖。當我開始使用它時,不用通過滑動進入prev/next視圖,它開始跳轉到看起來隨機的視圖。一旦我開始向前滑動時向後滑動...
是否有一個更合適的地方做預加載,只在頁面轉換完成後調用一次?而且,是否必須調用上面的行,或者有更可靠的方法來確保它進入正確的頁面?試圖在這裏進行聯繫感到沮喪。
// 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];
}
}
}
謝謝,這工作得很好!請參閱上面的更新以獲取解這個功能非常乾淨。 – Miro
Swift 3:func pageViewController(_ pageViewController:UIPageViewController,didFinishAnimating finished:Bool,previousViewControllers:[UIViewController],transitionCompleted completed:Bool) –