//
// scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 10, self.view.frame.size.height);
// [self.view addSubview:scrollView];
// scrollView.delegate = self;
// scrollView.pagingEnabled = YES;
// scrollView.showsHorizontalScrollIndicator = NO;
// for (int i = 0; i < 10; i++) {
// UIImage *image = [UIImage imageNamed:@"Default"];
// UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// imageView.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height);
// [scrollView addSubview:imageView];
//
mainScrollView.contentSize=CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
//[self.view addSubview:mainScrollView];
[mainScrollViewsetDelegate:self];
mainScrollView.pagingEnabled =YES;
mainScrollView.showsHorizontalScrollIndicator =NO;
viewOne.frame =CGRectMake(self.view.frame.size.width * 0, 0, self.view.frame.size.width,self.view.frame.size.height);
[mainScrollViewaddSubview:viewOne];
viewTwo.frame =CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width,self.view.frame.size.height);
[mainScrollViewaddSubview:viewTwo];
程序要像Spring一样在一屏中手指滑动实现多个页面的滑动切换可使用UIScrollView来实现
- - (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