2010-04-12 72 views
0

我遇到問題試圖讓pageControl示例代碼與旋轉一起工作。我設法讓它旋轉,但它不能正確加載,直到我開始滾動(然後它工作正常)。任何關於如何解決這個問題的想法?如果你想在action看到它,這裏是該項目的鏈接。旋轉UIScrollView時出現問題

此代碼基於Apple提供的PageControl示例。

這裏是代碼:

#import "ScrollingViewController.h" 
#import "MyViewController.h" 

@interface ScrollingViewController (PrivateMethods) 

- (void)loadScrollViewWithPage:(int)page; 

@end 

@implementation ScrollingViewController 
@synthesize scrollView; 
@synthesize viewControllers; 

- (void)viewDidLoad 
{ 
    amount = 5; 
    [super viewDidLoad]; 
    [self setupPage]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload 
{ 
    [scrollView release]; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)setupPage 
{ 
    NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
    for (unsigned i = 0; i < amount; i++) { 
     [controllers addObject:[NSNull null]]; 
    } 

    self.viewControllers = controllers; 
    [controllers release]; 

    // a page is the width of the scroll view 
    scrollView.pagingEnabled = YES; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * amount, 200); 

    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    [self loadScrollViewWithPage:0]; 
    [self loadScrollViewWithPage:1]; 
} 

#pragma mark - 
#pragma mark UIScrollViewDelegate stuff 

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView 
{ 
    if (pageControlIsChangingPage) { 
     return; 
    } 

    /* 
    * We switch page at 50% across 
    */ 
    CGFloat pageWidth = _scrollView.frame.size.width; 


    int dog = floor((_scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    // pageControl.currentPage = page; 

    [self loadScrollViewWithPage:dog - 1]; 
    [self loadScrollViewWithPage:dog]; 
    [self loadScrollViewWithPage:dog + 1]; 
} 

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

    MyViewController *controller = [viewControllers objectAtIndex:page]; 

    if ((NSNull *)controller == [NSNull null]) { 
     controller = [[MyViewController alloc] initWithPageNumber:page]; 
     [viewControllers replaceObjectAtIndex:page withObject:controller]; 
     [controller release]; 
    } 

    if (nil == controller.view.superview) { 
     CGRect frame = scrollView.frame; 

     frame.origin.x = frame.size.width * page; 

     frame.origin.y = 0; 
     controller.view.frame = frame; 
     [scrollView addSubview:controller.view]; 
    } 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    [self setupPage]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return YES; 
} 

@end 

回答

0

聽起來像它缺少一個-setNeedsDisplay:消息。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
[self setupPage]; 

// try this: 
[scrollView setNeedsDisplay:YES]; 
} 
+0

嗯,聽起來不錯,但我得到「的UIScrollView」不能爲「-setNeedsDisplay」作出迴應時,我嘗試..也許我需要做不同的東西嗎? – 2010-04-12 06:58:46

+0

oh整潔,[scrollView setNeedsDisplay];似乎工作,但它仍然有點偏離..例如,當它到達橫向模式的最後一頁,我仍然可以看到屏幕上的人像卡,但除此之外,這是一個很好的步驟! – 2010-04-12 07:10:01