force UIScrollView to update
this is the code:
-(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];
}
}
as you can see in this function we declare from start calling the thread with selector drillPagesFrom:(NSNumber*)page, which is defined ad follows:
-(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];
}
after various tests I've tried to force the redraw of the UIScrollView by calling the main thread, as you can notice in the code above
[self performSelectorOnMainThread:@selector(updateScrollView:) withObject:controller waitUntilDone:NO];
the function updateScrollView is
- (void) updateScrollView:(CharacterView *)controller {
[self.scrollView addSubview:[controller view]];
[self.scrollView setNeedsDisplay];
}
despite of all this code still waits to load all the pages before displaying anything n screen, which is very very annoying because you see blank space for about ~2 sec.
hoping that this explains the issue in a clear way,
I'm thanking you in advance.cheers
kWhat you want to use is a tiled scroll view. This will let you only have the content that needs to be displayed on screen loaded at once. Check out the ScrollViewSuite sample code on Apple's site.
in the end, I figured out the setNeedsDisplay made my day.
精彩评论