2013-08-20 45 views
3

我使用UIScrollViews雷Wenderlich的(马特·加洛韦的)教程: http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content如何更改Ray Wenderlich的UIScrollView教程的当前页面?

我在我的应用程序中使用的部分,“寻呼与UIScrollView的”。当你总是希望页面加载到0时,这很好用,但我希望当前页面是动态的。我将根据用户从前一个视图控制器(这是一个名为“listIndex”的NSInteger)中选择的缩略图传递此信息。

如何将当前页面更改为我的listIndex?我试图改变

self.pageControl.currentPage = 0; 

self.pageControl.currentPage = self.listIndex; 
在viewDidLoad中

,但这似乎并没有帮助 - 这可能是因为它得到复位的loadVisiblePages?

非常感谢您的帮助!下面的代码:

- (void)loadVisiblePages { 
    // First, determine which page is currently visible 
    CGFloat pageWidth = self.scrollView.frame.size.width; 

    NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth)/(pageWidth * 2.0f)); 

    // Update the page control 
    self.pageControl.currentPage = page; 


    // Work out which pages you want to load 
    NSInteger firstPage = page - 1; 
    NSInteger lastPage = page + 1; 

    // Purge anything before the first page 
    for (NSInteger i=0; i<firstPage; i++) { 
     [self purgePage:i]; 
    } 

    // Load pages in our range 
    for (NSInteger i=firstPage; i<=lastPage; i++) { 
     [self loadPage:i]; 
    } 

    // Purge anything after the last page 
     for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) { 
      [self purgePage:i]; 
     } 
} 


- (void)purgePage:(NSInteger)page { 
    if (page < 0 || page >= self.pageImages.count) { 
     // If it's outside the range of what you have to display, then do nothing 
     return; 
    } 

    // Remove a page from the scroll view and reset the container array 
    UIView *pageView = [self.pageViews objectAtIndex:page]; 
    if ((NSNull*)pageView != [NSNull null]) { 
     [pageView removeFromSuperview]; 
     [self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]]; 
    } 
} 

- (void)loadPage:(NSInteger)page { 
    if (page < 0 || page >= self.pageImages.count) { 
     // If it's outside the range of what you have to display, then do nothing 
     return; 
    } 

    // 1 
    UIView *pageView = [self.pageViews objectAtIndex:page]; 
    if ((NSNull*)pageView == [NSNull null]) { 
     // 2 
     CGRect frame = self.scrollView.bounds; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0.0f; 

     // 3 
     UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]]; 
     newPageView.contentMode = UIViewContentModeScaleAspectFit; 
     newPageView.frame = frame; 
     [self.scrollView addSubview:newPageView]; 
     // 4 
     [self.pageViews replaceObjectAtIndex:page withObject:newPageView]; 
    } 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    // Load the pages that are now on screen 
    [self loadVisiblePages]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSMutableString *myHTML = [[NSMutableString alloc] initWithString:@"<html><head><style type='text/css'> p {font-family:sans-serif; text-shadow: 3px 4px 5px #000; color:#FFF; position: absolute; bottom:0; font-size:10pt; font-weight:bold; margin-bottom:5px;}</style><body><p>"]; 

    [myHTML appendString:self.caption]; 

    [myHTML appendString:@"</p></body></html>"]; 

    [captionWebView loadHTMLString:myHTML baseURL:nil]; 

    self.pageImages = [NSMutableArray array]; 

    for (int i=0; i < [imageList count]; i++) { 
     NSString *path = [(Image *)[imageList objectAtIndex:i] path]; 
     [self.pageImages addObject:[UIImage imageNamed:path]]; 
    } 


    NSInteger pageCount = self.pageImages.count; 

    // 2 
    self.pageControl.currentPage = 0; 
    //self.pageControl.currentPage = self.listIndex; //Tried this 
    self.pageControl.numberOfPages = pageCount; 

    // 3 
    self.pageViews = [[NSMutableArray alloc] init]; 
    for (NSInteger i = 0; i < pageCount; ++i) { 
     [self.pageViews addObject:[NSNull null]]; 
    } 




} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 


    // add this in order UIScrollView work with Auto layout 

    if (IS_WIDESCREEN) { 

     self.scrollView.frame = CGRectMake(0, 0, 320, 468); 

    } else { 

     self.scrollView.frame = CGRectMake(0, 0, 320, 380); 

    } 

    // 4 
    CGSize pagesScrollViewSize = self.scrollView.frame.size; 
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height); 

    // 5 
    [self loadVisiblePages]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 

    // add this in order UIScrollView work with Auto layout 

    if (IS_WIDESCREEN) { 

     self.scrollView.contentSize = CGSizeMake(320, 468); 

    } else { 

     self.scrollView.contentSize = CGSizeMake(320, 380); 

    } 

} 

回答

2

尝试设置滚动视图在viewWillAppear:初始contentOffset

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // Set up the content size of the scroll view 
    CGSize pagesScrollViewSize = self.scrollView.frame.size; 
    self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height); 

    // Set initial content offset of scrollview 
    self.scrollView.contentOffset = CGPointMake(pagesScrollViewSize.width * self.listIndex, self.scrollView.contentOffset.y); 

    // Load the initial set of pages that are on screen 
    [self loadVisiblePages]; 
} 
+0

完美!非常感谢。这正是我期待的行为。如果它始终必须设置为0,你是否认为currentPage的要点?我想我不明白他那个变量的原意。 – monalisa717

+0

'self.pageControl.currentPage'确定'UIPageControl'中白点的索引。它在'viewDidLoad:'中初始化为0,但这并不重要,因为它根据scrollview的'contentOffset'在'loadVisiblePages'中设置为正确的值。这就是为什么在调用loadVisiblePages之前在'viewWillAppear:'中设置'contentOffset'的原因,因为'currentPage'将被设置为这个方法中的正确页面。 –

相关问题