Codesnipp.it Social Code Sharing

Scott Rapson

Horizontal UIScroller with Paging control

by Scott Rapson on Feb 18, 2011

/* Code allows paging control of a UIScrollView with the page indicators. The page indicators and ScrollView are both already added in Interface builder, these are then wired to the outlets. My code is for a how-to page, which scrolls any number of images in the resource structure named image# (the # represents a number from 1 to anything) This code is in my HowViewController classes, hence those references. For an example of this in an app, download Ball-TaP from the AppStore. */ //your header should look something like this @protocol HowViewControllerDelegate; // declare a new protocol @interface HowViewController : UIViewController <UIScrollViewDelegate> { id <HowViewControllerDelegate> delegate; // this class's delegate IBOutlet UIScrollView* scrollView; IBOutlet UIPageControl* pageControl; BOOL pageControlIsChangingPage; } @property(nonatomic, assign) id <HowViewControllerDelegate> delegate; @property (nonatomic, retain) UIView *scrollView; @property (nonatomic, retain) UIPageControl* pageControl; //menu button declaration -(IBAction)menu; /* for pageControl */ - (IBAction)changePage:(id)sender; /* internal */ - (void)setupPage; @end //////////////////////////////////// //your method should look something like this @implementation HowViewController @synthesize scrollView; @synthesize pageControl; @synthesize delegate; // generate getter and setter for delegate // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [self setupPage]; [super viewDidLoad]; } -(IBAction)menu; { MainViewController *controller = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil]; //controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:controller animated:YES]; [controller release]; } //START SCROLLER CODE - (void)setupPage { scrollView.delegate = self; [self.scrollView setBackgroundColor:[UIColor blackColor]]; [scrollView setCanCancelContentTouches:NO]; scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; scrollView.clipsToBounds = YES; scrollView.scrollEnabled = YES; scrollView.pagingEnabled = YES; NSUInteger nimages = 0; CGFloat cx = 0; for (; ; nimages++) { NSString *imageName = [NSString stringWithFormat:@"image%d.png", (nimages + 1)]; UIImage *image = [UIImage imageNamed:imageName]; if (image == nil) { break; } UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; CGRect rect = imageView.frame; rect.size.height = image.size.height; rect.size.width = image.size.width; rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx; rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2); imageView.frame = rect; [scrollView addSubview:imageView]; [imageView release]; cx += scrollView.frame.size.width; } self.pageControl.numberOfPages = nimages; [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)]; } - (void)scrollViewDidScroll:(UIScrollView *)_scrollView { if (pageControlIsChangingPage) { return; } /* * We switch page at 50% across */ CGFloat pageWidth = _scrollView.frame.size.width; int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; pageControl.currentPage = page; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView { pageControlIsChangingPage = NO; } - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)_scrollView { pageControlIsChangingPage = NO; } - (IBAction)changePage:(id)sender { /* * Change the scroll view */ CGRect frame = scrollView.frame; frame.origin.x = frame.size.width * pageControl.currentPage; frame.origin.y = 0; [scrollView scrollRectToVisible:frame animated:YES]; /* * When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off */ pageControlIsChangingPage = YES; } // END SCROLLER CODE //continue with the release code, didRecieveMemoryWarning, dealloc etc

Can't see the comments? Please login first :)