UIPageControl的简单使用

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];  
  6.     _scrollView.userInteractionEnabled = YES;  
  7.     _scrollView.directionalLockEnabled = YES;  
  8.     _scrollView.pagingEnabled = YES;  
  9.     _scrollView.showsVerticalScrollIndicator = NO;  
  10.     _scrollView.showsHorizontalScrollIndicator = NO;  
  11.     _scrollView.delegate = self;  
  12.       
  13.     CGRect frame = self.view.frame;  
  14.     frame.origin.y = 0.0f;  
  15.     UIView* viewFirst = [[[UIView alloc] initWithFrame:frame] autorelease];  
  16.     viewFirst.backgroundColor = [UIColor greenColor];  
  17.       
  18.     frame.origin.x += self.view.frame.size.width;  
  19.     UIView* viewSecond = [[[UIView alloc] initWithFrame:frame] autorelease];  
  20.     viewSecond.backgroundColor = [UIColor blueColor];  
  21.       
  22.     frame.origin.x += self.view.frame.size.width;  
  23.     UIView* viewThird = [[[UIView alloc] initWithFrame:frame] autorelease];  
  24.     viewThird.backgroundColor = [UIColor redColor];  
  25.     _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);  
  26.     [_scrollView addSubview:viewFirst];  
  27.     [_scrollView addSubview:viewSecond];  
  28.     [_scrollView addSubview:viewThird];  
  29.       
  30.     CGSize sizePageControl = CGSizeMake(120, 40);  
  31.     CGRect framePageControl = CGRectMake((self.view.frame.size.width-sizePageControl.width)/2, (self.view.frame.size.height-sizePageControl.height-40), sizePageControl.width, sizePageControl.height);  
  32.     _pageControl = [[UIPageControl alloc] initWithFrame:framePageControl];  
  33.     _pageControl.hidesForSinglePage = YES;  
  34.     _pageControl.userInteractionEnabled = NO;  
  35.     _pageControl.backgroundColor = [UIColor clearColor];  
  36.     _pageControl.numberOfPages = 3;  
  37.       
  38.     [self.view addSubview:_scrollView];  
  39.     [self.view addSubview:_pageControl];  
  40.       
同时你的ViewController 要实现UIScrollViewDelegate 协议的如下方法, 根据scroll 更新UIPageControl(当然如过你用到UIPageControl的话就无所谓了...)
  1. - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  2. {  
  3.     int index = fabs(_scrollView.contentOffset.x)/self.view.frame.size.width;  
  4.       
  5.     _pageControl.currentPage = index;  
  6. }  


下面是效果图:

 

 

转至http://blog.csdn.net/z888c/article/details/6563662



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值