开发者

force UIScrollView to update

I'm stuck with this problem I have a serie of pages of the CharacterView class that are going straight into a UIScrollView.

the loading part was very performance intensive, because of this reason I tried to manage it with the aid of a threading scenario.

My intention was to load the requested page (es. the 5th page out of 10) and starting to load all the pages of this UIScrollView from there and showing it immediately on screen, then recurring somehow and load the +1 / -1 (respectively 6 and 4) and show'em on screen , then in the second recursion load the +2 / -2 ( 7th and 3rd ) and show'em on screen and so on..

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

k


What 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜