- - (void)viewDidLoad
- {
- [super viewDidLoad];
- _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
- _scrollView.userInteractionEnabled = YES;
- _scrollView.directionalLockEnabled = YES;
- _scrollView.pagingEnabled = YES;
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.delegate = self;
- CGRect frame = self.view.frame;
- frame.origin.y = 0.0f;
- UIView* viewFirst = [[[UIView alloc] initWithFrame:frame] autorelease];
- viewFirst.backgroundColor = [UIColor greenColor];
- frame.origin.x += self.view.frame.size.width;
- UIView* viewSecond = [[[UIView alloc] initWithFrame:frame] autorelease];
- viewSecond.backgroundColor = [UIColor blueColor];
- frame.origin.x += self.view.frame.size.width;
- UIView* viewThird = [[[UIView alloc] initWithFrame:frame] autorelease];
- viewThird.backgroundColor = [UIColor redColor];
- _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);
- [_scrollView addSubview:viewFirst];
- [_scrollView addSubview:viewSecond];
- [_scrollView addSubview:viewThird];
- CGSize sizePageControl = CGSizeMake(120, 40);
- CGRect framePageControl = CGRectMake((self.view.frame.size.width-sizePageControl.width)/2, (self.view.frame.size.height-sizePageControl.height-40), sizePageControl.width, sizePageControl.height);
- _pageControl = [[UIPageControl alloc] initWithFrame:framePageControl];
- _pageControl.hidesForSinglePage = YES;
- _pageControl.userInteractionEnabled = NO;
- _pageControl.backgroundColor = [UIColor clearColor];
- _pageControl.numberOfPages = 3;
- [self.view addSubview:_scrollView];
- [self.view addSubview:_pageControl];
- }
同时你的ViewController 要实现UIScrollViewDelegate 协议的如下方法, 根据scroll 更新UIPageControl(当然如过你用到UIPageControl的话就无所谓了...)
- - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- int index = fabs(_scrollView.contentOffset.x)/self.view.frame.size.width;
- _pageControl.currentPage = index;
- }
下面是效果图:
转至http://blog.csdn.net/z888c/article/details/6563662