views:

20

answers:

0

I have been attempting to make my pdf reader app remember which page its on when changing orientation (at the moment it goes back to the first page every time the orientation switches). Here is my working code:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{         NSLog(@"willAnimateRotationToInterfaceOrientation");
UIInterfaceOrientation o = self.interfaceOrientation;
if ((o == UIInterfaceOrientationPortrait) || (o == UIInterfaceOrientationPortraitUpsideDown))   {D = 1;[pagingScrollView removeFromSuperview];[self setPaging];NSLog(@"D1");}
if ((o == UIInterfaceOrientationLandscapeLeft) || (o == UIInterfaceOrientationLandscapeRight))  {D = 2;[pagingScrollView removeFromSuperview];[self setPaging];NSLog(@"D2");}}

//-----------------------------------------------------------------------------*
- (void)viewDidLoad {                                                           NSLog(@"Qvc viewDidLoad");
D = 1;
[self setPaging];
}

//-----------------------------------------------------------------------------*
- (void)setPaging {                                                             NSLog(@"Qvc setPaging");
recycledPages = [[NSMutableSet alloc] init];
visiblePages  = [[NSMutableSet alloc] init];
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:F];
if (D == 1) {                                                               NSLog(@"Qvc setPaging D1");
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor whiteColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(320.0f * [self pdfPageCount], 435.0f);
    pagingScrollView.delegate = self;
    pagingScrollView.clipsToBounds = YES;
    pagingScrollView.bounces = YES;
    [self.view addSubview:pagingScrollView];
    [self tilePages];
    [self.isvL removeFromSuperview];
    [pagingScrollView addSubview:self.isvP];
    }

else if (D == 2) {                                                          NSLog(@"Qvc setPaging D2");
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor whiteColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(480.0f * [self pdfPageCount], 300.0f);
    pagingScrollView.delegate = self;
    pagingScrollView.clipsToBounds = YES;
    pagingScrollView.bounces = YES;
    [self.view addSubview:pagingScrollView];
    [self tilePages];
    [self.isvP removeFromSuperview];
    [pagingScrollView addSubview:self.isvL];
    }}

//-----------------------------------------------------------------------------*
- (CGRect)frameForPagingScrollView {                                            NSLog(@"Qvc frameForPagingScrollView");
CGRect frame;
if (D == 1) {                                                               NSLog(@"Qvc frameForPagingScrollView D1");
    frame = CGRectMake(0.0f, 0.0f, 320.0f, 435.0f);
    frame.size.width += (0);}
else if (D == 2) {                                                          NSLog(@"Qvc frameForPagingScrollView D2");
    frame = CGRectMake(0.0f, 0.0f, 480.0f, 300.0f);
    frame.size.width += (0);}
return frame;}


//-----------------------------------------------------------------------------*
- (void)tilePages {                                                             NSLog(@"Qvc tilePages");
if (D == 1) {                                                               NSLog(@"Qvc tilePages D1");
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
    // Recycle no-longer-visible pages 
    for (ISVportrate *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];}}
    [visiblePages minusSet:recycledPages];
    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ISVportrate *page = [self dequeueRecycledPageP];
            if (page == nil) {
                page = [[[ISVportrate alloc] init] autorelease];}
        [self configurePageP:page forIndex:index];
        [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}}
else if (D == 2) {                                                          NSLog(@"Qvc tilePages D2");
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
    // Recycle no-longer-visible pages 
    for (ISVLandscape *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];}}
    [visiblePages minusSet:recycledPages];
    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ISVLandscape *page = [self dequeueRecycledPageL];
            if (page == nil) {
                page = [[[ISVLandscape alloc] init] autorelease];}
            [self configurePageL:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}}}

the following changes (below) make the app remember which page its on when changing orientation but seemingly only because paging scrollview is not deleted, also the frame for pagingScrollView seems to be set at portrait but also showing characteristics of its landscape setting (frame size setting heigh-wise wont let you scroll vertically unless zoomed in). It seems that whenever I alter the line '- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{' pagingScrollView is not deleted.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation /*duration:(NSTimeInterval)duration*/ forIndex:(NSUInteger)index{          NSLog(@"willAnimateRotationToInterfaceOrientation");
UIInterfaceOrientation o = self.interfaceOrientation;
if ((o == UIInterfaceOrientationPortrait) || (o == UIInterfaceOrientationPortraitUpsideDown))   {D = 1;[pagingScrollView removeFromSuperview];/*[self setPaging];int index;*/[self setPaging:index];NSLog(@"D1");}
if ((o == UIInterfaceOrientationLandscapeLeft) || (o == UIInterfaceOrientationLandscapeRight))  {D = 2;[pagingScrollView removeFromSuperview];/*[self setPaging];int index;*/[self setPaging:index];NSLog(@"D2");}}



//-----------------------------------------------------------------------------*
- (void)viewDidLoad {                                                           NSLog(@"Qvc viewDidLoad");
D = 1;
//[self setPaging];
int index;[self setPaging:index];
[self tilePages];//b
}

//-----------------------------------------------------------------------------*
//- (void)setPaging {                                                               NSLog(@"Qvc setPaging");
- (void)setPaging:(NSUInteger)index {
recycledPages = [[NSMutableSet alloc] init];
visiblePages  = [[NSMutableSet alloc] init];
CGRect F;
F = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:F];
if (D == 1) {                                                               NSLog(@"Qvc setPaging D1");
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor whiteColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(320.0f * [self pdfPageCount], 435.0f);
    pagingScrollView.delegate = self;
    pagingScrollView.clipsToBounds = YES;
    pagingScrollView.bounces = YES;
    [self.view addSubview:pagingScrollView];
    //[self tilePages];
    [self.isvL removeFromSuperview];
    [pagingScrollView addSubview:self.isvP];
    for (ISVportrate *page in visiblePages) {
        int index;
        if (page.index == index) {
            ISVportrate *page = [self dequeueRecycledPageP];
            if (page == nil) {
                page = [[[ISVportrate alloc] init] autorelease];}
            [self configurePageP:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}}
else if (D == 2) {                                                          NSLog(@"Qvc setPaging D2");
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor whiteColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(480.0f * [self pdfPageCount], 300.0f);
    pagingScrollView.delegate = self;
    pagingScrollView.clipsToBounds = YES;
    pagingScrollView.bounces = YES;
    [self.view addSubview:pagingScrollView];
    //[self tilePages];
    for (ISVLandscape *page in visiblePages) {
        int index;
        if (page.index == index) {
            ISVLandscape *page = [self dequeueRecycledPageL];
            if (page == nil) {
                page = [[[ISVLandscape alloc] init] autorelease];}
            [self configurePageL:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}
    [self.isvP removeFromSuperview];
    [pagingScrollView addSubview:self.isvL];
    }}

//-----------------------------------------------------------------------------*
- (CGRect)frameForPagingScrollView {                                            NSLog(@"Qvc frameForPagingScrollView");
CGRect frame;
if (D == 1) {                                                               NSLog(@"Qvc frameForPagingScrollView D1");
    frame = CGRectMake(0.0f, 0.0f, 320.0f, 435.0f);
    frame.size.width += (0);}
else if (D == 2) {                                                          NSLog(@"Qvc frameForPagingScrollView D2");
    frame = CGRectMake(0.0f, 0.0f, 480.0f, 300.0f);
    frame.size.width += (0);}
return frame;}



//-----------------------------------------------------------------------------*
- (void)tilePages {                                                             NSLog(@"Qvc tilePages");
if (D == 1) {                                                               NSLog(@"Qvc tilePages D1");
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
    // Recycle no-longer-visible pages 
    for (ISVportrate *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];}}
    [visiblePages minusSet:recycledPages];
    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ISVportrate *page = [self dequeueRecycledPageP];
            if (page == nil) {
                page = [[[ISVportrate alloc] init] autorelease];}
        [self configurePageP:page forIndex:index];
        [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}}
else if (D == 2) {                                                          NSLog(@"Qvc tilePages D2");
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
    // Recycle no-longer-visible pages 
    for (ISVLandscape *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];}}
    [visiblePages minusSet:recycledPages];
    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ISVLandscape *page = [self dequeueRecycledPageL];
            if (page == nil) {
                page = [[[ISVLandscape alloc] init] autorelease];}
            [self configurePageL:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];}}}}

Any idea on how to get this to remember the page?

Thanks

Ben.