我被這個問題困住了我有一系列的CharacterView類直接進入UIScrollView。
加載部分是非常性能密集的,因爲這個原因,我試圖通過線程場景的幫助來管理它。
我的意圖是加載所請求的頁面(例如第10頁中的第5頁)並開始加載此UIScrollView的所有頁面並立即在屏幕上顯示,然後以某種方式重複並加載+1/-1 (分別爲6和4)和屏幕上的顯示,然後在第二次遞歸中加載+2/-2(第7和第3)和show'em屏幕上等。強制UIScrollView更新
這是代碼:
-(void)loadPagesDrillingFromPage:(NSInteger) page {
self.viewControllers = [[[NSMutableArray alloc] init] autorelease];
for (unsigned i = 0; i < [pageIds count]; i++) {
[self.viewControllers addObject:[NSNull null]];
}
pageControl.numberOfPages = [pageIds count];
didPreparePagesContainer = YES;
if (kPRELOADALLPAGES && !threadIsLoadingPages) {
threadIsLoadingPages = YES;
NSNumber *startingPage = [NSNumber numberWithInt:page];
NSMutableArray *preloadedPages = [[[NSMutableArray alloc] init] autorelease];
[NSThread detachNewThreadSelector:@selector(drillPagesFrom:) toTarget:self withObject:startingPage];
}
}
,你可以在此功能中看到我們從開始調用與選擇drillPagesFrom線程聲明:(NSNumber的*)頁,其定義廣告如下:
-(void)drillPagesFrom:(NSNumber *) page {
NSInteger pg = [page intValue];
CharacterView *controller = [self.viewControllers objectAtIndex:pg];
if((NSNull *)controller != [NSNull null])
return;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"DRILLING page %i", pg);
controller = [[CharacterView alloc]
initWithNibName:@"CharacterView"
bundle:[NSBundle mainBundle]];
controller.didLoad = NO;
controller.position = pg;
controller.containerItemsCount = [self.pageIds count];
controller.delegate = self;
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * pg;
frame.origin.y = 0;
controller.view.frame = frame;
[controller setCat_name:self.cat_name];
[controller fetchCharacter:[self.pageIds objectAtIndex:pg] WithMOC:self._context];
[self.viewControllers replaceObjectAtIndex:pg withObject:controller];
[self performSelectorOnMainThread:@selector(updateScrollView:) withObject:controller waitUntilDone:NO];
[controller release];
if(pg > 1)// && !threadReachedFirstPage)
{
NSNumber *prevPg = [NSNumber numberWithInt:pg-1];
[self drillPagesFrom:prevPg];
}
if(pg < [self.pageIds count]-1)// && !threadReachedLastPage)
{
NSNumber *nextPg = [NSNumber numberWithInt:pg+1];
[self drillPagesFrom:nextPg];
}
[pool release];
}
各種測試後,我已經嘗試通過調用主線程,強制的UIScrollView的重繪,你可以在上面的代碼中
[self performSelectorOnMainThread:@selector(updateScrollView:) withObject:controller waitUntilDone:NO];
注意函數updateScrollView是
- (void) updateScrollView:(CharacterView *)controller {
[self.scrollView addSubview:[controller view]];
[self.scrollView setNeedsDisplay];
}
儘管所有這些代碼仍然等待顯示任何n屏幕之前加載所有頁面,這是非常非常煩人的,因爲你看到約2秒的空白空間。
希望能夠以一種明確的方式解釋問題,
我在此先感謝您。
歡呼
ķ
那麼,這是什麼問題? – Altealice 2011-02-17 17:28:34