Issue when rotating a UIScrollView

Posted by leachianus.gecko on Stack Overflow See other posts from Stack Overflow or by leachianus.gecko
Published on 2010-04-12T02:05:39Z Indexed on 2012/10/01 21:38 UTC
Read the original article Hit count: 165

I am having issues trying to get the pageControl sample code to work with rotation. I managed to get it to rotate but it does not visually load correctly until I start to scroll (then it works fine). Any Idea on how I can fix this problem? Here is a link to the project if you want to see it in action.

This code is based off the PageControl example apple has provided.

here is the code:

#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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c