2010-10-04 4 views
7

J'ai une application basée sur l'exemple PageControl d'Apple. La première fois que la vue se charge, la vue de défilement est chargée avec la page 0 et la page 1. Chaque fois qu'un défilement est lancé, la méthode scrollViewDidScroll doit être appelée par UIKit correct? Lors du lancement d'un défilement de la page 0 à la page 1, l'application doit charger la page-1, la page et la page + 1 (pour éviter les clignotements pendant le défilement).Mon application appelle scrollViewDidScroll 19 fois

Mon application semble appeler scrollViewDidScroll 19 fois et ma méthode loadScrollViewWithPage: 19 fois chacune avec la page 0 et la page 1, avant d'arriver finalement aux pages 1 et 2, puis elle se bloque.

Ce sont les méthodes:

- (void)scrollViewDidScroll:(UIScrollView *)sender { 
    NSLog(@"scrollviewdidscroll"); 
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in 
    // which a scroll event generated from the user hitting the page control triggers updates from 
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used. 
    if (pageControlUsed) { 
     // do nothing - the scroll was initiated from the page control, not the user dragging 
     return; 
    } 

    // Switch the indicator when more than 50% of the previous/next page is visible 
    CGFloat pageWidth = scrollView.frame.size.width; 
    int page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage = page; 

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling) 
    [self loadScrollViewWithPage:page - 1]; 
    [self loadScrollViewWithPage:page]; 
    [self loadScrollViewWithPage:page + 1]; 

    // A possible optimization would be to unload the views+controllers which are no longer visible 
} 

- (void)loadScrollViewWithPage:(int)page { 
    if (page < 0) return; 
    if (page >= kNumberOfPages) return; 

    NSLog(@"page: %i", page); 

    // replace the placeholder if necessary 
    KeyboardViewController *controller = [viewControllers objectAtIndex:page]; 
    if ((NSNull *)controller == [NSNull null]) { 
     controller = [[KeyboardViewController alloc] initWithPageNumber:page]; 
     [viewControllers replaceObjectAtIndex:page withObject:controller]; 
     [controller release]; 
    } 

    // add the controller's view to the scroll view 
    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    frame.size.height = scrollView.frame.size.height; 
    controller.view.frame = frame; 
    [scrollView setAutoresizesSubviews:YES]; 
    [scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; 
    [scrollView addSubview:controller.view]; 
} 

Pourquoi scrollViewDidScroll obtenir appelé tant de fois?

Merci

Répondre

24

scrollViewDidScroll: est appelé à chaque fois que les bornes de défilement changement. Cela signifie qu'il est appelé pendant le défilement, ainsi que quand il commence. Vous pouvez essayer scrollViewWillBeginDragging: à la place.

Questions connexes