In my TestApp, I created the Carousel class, which allowed me to easily create a context menu using UIPageControl (just by creating an instance of the Carousel class).
- Carousel is a subclass of UIView
- In init, it creates a UIView containing a UIScrollView, UIPageControl
- I can add more UIViews to the scroll view
I don't know if this is suitable for this, but my example worked well in my TestApp. Skipping between pages works fine, and displaying the current page in UIPageControl is correct.
If there was not a single problem: UIPageControl sometimes responds to clicks / tags (I have only tested in Simulator so far!), Sometimes it is not. Let them say that most of the time this is not so. I could not find out when this happens, for me it's just random ...
As you can see below, I added
[pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
into my code. I thought this would make the right handling of the taps? But unfortunately, pageChange is not always called (therefore, the value of UIPageControl does not change every time I click).
I would appreciate any information on this, because so far I have not been able to find a solution.
This is what I have so far:
Carousel.h
Carousel.m
#import "Carousel.h" @implementation Carousel - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Create a scroll view scrollView = [[UIScrollView alloc] init]; [self addSubview:scrollView]; scrollView.delegate = (id) self; // Init Page Control pageControl = [[UIPageControl alloc] init]; [pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged]; [self addSubview:pageControl]; } return self; } - (IBAction)pageChange:(id)sender { CGRect frame = scrollView.frame; frame.origin.x = frame.size.width * pageControl.currentPage; frame.origin.y = 0; [scrollView scrollRectToVisible:frame animated:TRUE]; NSLog(@"%i", pageControl.currentPage); } - (void)addView:(UIView *)view { [scrollView addSubview:view]; } - (void)createPageControlAt:(CGRect)cg { pageControl.frame = cg; } - (void)setTotalPages:(NSUInteger)pages { pageControl.numberOfPages = pages; } - (void)setCurrentPage:(NSUInteger)current { pageControl.currentPage = current; } - (void)createScrollViewAt:(CGRect)cg { [scrollView setPagingEnabled:TRUE]; scrollView.frame = cg; scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*pageControl.numberOfPages, scrollView.frame.size.height); [scrollView setShowsHorizontalScrollIndicator:FALSE]; [scrollView setShowsVerticalScrollIndicator:FALSE]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { float frac = scrollView.contentOffset.x / scrollView.frame.size.width; NSUInteger page = lround(frac); pageControl.currentPage = page; } @end
ViewController.m (somewhere in viewDidLoad)
Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; for (int i=0; i<5; i++) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 320, 0, 320, 420)]; UIColor *color; if(i%3==0) color = [UIColor blueColor]; else if(i%3==1) color = [UIColor redColor]; else color = [UIColor purpleColor]; view.backgroundColor = color; [carousel addView:view]; view = nil; } [carousel setTotalPages:5]; [carousel setCurrentPage:0]; [carousel createPageControlAt:CGRectMake(0,420,320,40)]; [carousel createScrollViewAt:CGRectMake(0,0,320,420)];