views:

867

answers:

0

Hi all,

I have implemented a circular scroll view.

It's working fine , but after sometimes the scroll view delegates are not getting called.

I have attached the code , what is the bug in it ....

//
//  MyScrollViewViewController.h
//  MyScrollView
//
//  Created by Biranchi on 26/05/09.
//  Copyright PurpleTalk 2009. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyScrollViewViewController : UIViewController <UIScrollViewDelegate> {

    IBOutlet UIScrollView *scrollView;
    IBOutlet UIView *view1;
    IBOutlet UIView *view2;
    IBOutlet UIView *view3;


    IBOutlet UILabel *label1;
    IBOutlet UILabel *label2;
    IBOutlet UILabel *label3;


    int viewAtIndex;
    int currentOffset;
    int firstOffset, lastOffset;
    int currentContentInset;
    int tempOffset;


    NSMutableArray *arr;
}

@end



//
//  MyScrollViewViewController.m
//  MyScrollView
//
//  Created by Biranchi on 26/05/09.
//  Copyright PurpleTalk 2009. All rights reserved.
//

#import "MyScrollViewViewController.h"

@implementation MyScrollViewViewController



// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }


    return self;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


//static int j = 0;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    arr = [[NSMutableArray alloc] init];

    [arr addObject:view1];
    [arr addObject:view2];
    [arr addObject:view3];

    //NSLog(@"arr : %@", arr);

    scrollView.minimumZoomScale = 1.0f;
    scrollView.maximumZoomScale = 2.0f;
    scrollView.delegate = self;
    //scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


    firstOffset = 0;
    currentOffset = 320;
    lastOffset = 640;
    currentContentInset = 320;

    CGRect frame;

    //Scroll View

    frame = [view1 frame];
    [view1 setBackgroundColor:[UIColor redColor]];
    frame.origin.x = 0;
    [view1 setFrame:frame];

    frame = [view2 frame];
    [view2 setBackgroundColor:[UIColor greenColor]];
    frame.origin.x = 320;
    [view2 setFrame:frame];

    frame = [view3 frame];
    [view3 setBackgroundColor:[UIColor blueColor]];
    frame.origin.x = 640;
    [view3 setFrame:frame];

    [scrollView addSubview:view1];
    [scrollView addSubview:view2];
    [scrollView addSubview:view3];

    [scrollView setContentSize:CGSizeMake(3*320, 480)];
    [scrollView setContentOffset: CGPointMake(currentOffset,0) animated:NO];

    [self.view addSubview:scrollView];

    //NSLog(@"Self.view Subviews : %@", [self.view subviews]);
}


#pragma mark -
#pragma mark ScrollView Delegate 



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1
{
    NSLog(@"Will begin dragging ===");

    CGPoint offset = [scrollView1 contentOffset];
    tempOffset = offset.x;
    NSLog(@"Offset : %@", NSStringFromCGPoint(offset));

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1
{
    NSLog(@"ScrollView did end decelerating ");

    int i, result;

    CGPoint offset = [scrollView1 contentOffset];
    currentOffset = offset.x;
    NSLog(@"Offset : %@", NSStringFromCGPoint(offset));

    if( ( (currentOffset - tempOffset) > 0 ) || ( (currentOffset == tempOffset) && (currentOffset == lastOffset) ) )
    {
     NSLog(@"Show next page , current : %d, temp : %d", currentOffset, tempOffset); 


     for(i=0; i<[arr count]; i++)
     {
      if( [[arr objectAtIndex:i] frame].origin.x == currentOffset )
      {
       NSLog(@"Current view on screen : %d", i);
       break;
      }
     }


     NSLog(@"Before Current offset : %d, last offset : %d", currentOffset, lastOffset);

     if( currentOffset == lastOffset )
     {
      result = i-2;

      if(result < 0)
      {
       result += 3; 
      }


      NSLog(@"Change the frame of object at index : %d", result);

      [[arr objectAtIndex:result] setFrame:CGRectMake(currentOffset + 320, 0, 320, 480)];
      [scrollView1 setContentSize:CGSizeMake(currentOffset+640,480) ];
      lastOffset += 320;
      firstOffset += 320;
     }

     NSLog(@"Current offset : %d, first offset : %d , last offset : %d", currentOffset, firstOffset,  lastOffset);

    }
    else if(  ( (currentOffset - tempOffset) < 0 ) || ( (currentOffset == tempOffset) && (currentOffset == firstOffset) ) ) 
    {
     //NSLog(@"Show prev page"); 


     for(i=0; i<[arr count]; i++)
     {
      if( [[arr objectAtIndex:i] frame].origin.x == currentOffset )
      {
       NSLog(@"==Current view on screen : %d", i);
       break;
      }
     }


     NSLog(@"==Before Current offset : %d, First offset : %d", currentOffset, firstOffset);

     if( currentOffset == firstOffset )
     {
      result = i+2;

      if(result >= 3)
      {
       result -= 3; 
      }


      NSLog(@"==Change the frame of object at index : %d", result);

      [[arr objectAtIndex:result] setFrame:CGRectMake(currentOffset - 320, 0, 320, 480)];

      UIEdgeInsets contentInset = {0,currentContentInset,0,0};
      [scrollView1 setContentInset:contentInset ];

      firstOffset -= 320;
      currentContentInset += 320;
      lastOffset -= 320;
     }

     NSLog(@"==Current offset : %d, first offset : %d, Last Offset : %d", currentOffset, firstOffset, lastOffset);

    }
    else 
    {
     NSLog(@"Error in offset");
    }

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1
{
    return [[scrollView1 subviews] objectAtIndex:viewAtIndex]; 
}


- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView1 withView:(UIView *)view atScale:(float)scale
{
    //NSLog(@"View Zoomed : %@, scale : %f", view, scale);
}

#pragma mark -


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


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

@end