pageControl的实现

  1 - (void)viewDidLoad
  2 {
  3     [super viewDidLoad];
  4     // Do any additional setup after loading the view.
  5 
  6 //获取当前view大小
  7     CGRect bounds = self.view.frame;
  8     /*
  9      1.根视图控制器self.view坐标(0, 20, 320, 460);  *此处针对的坐标系是window
 10      2.视图控制器,只能添加到window上,因为视图控制器带有20高的白条
 11      3.frame相对于父视图
 12      4.bounds相对于自身
 13      */
 14     
 15 //创建滚动视图
 16     UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(bounds.origin.x, 0, bounds.size.width, bounds.size.height)];
 17     //设置大小,使用5张图所以乘以5
 18     scrollView.contentSize = CGSizeMake(bounds.size.width * 5, bounds.size.height);
 19     //设置翻页
 20     scrollView.pagingEnabled = YES;
 21     //设置代理
 22     scrollView.delegate = self;
 23     //tag
 24     scrollView.tag = 102;
 25     //添加图片
 26     for (int i = 0; i < 5; i++)
 27     {
 28         //设置图片
 29         UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"soul%d.jpg", i]];
 30         //创建图片视图
 31         UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(bounds.origin.x + bounds.size.width * i, 0, bounds.size.width, bounds.size.height)];
 32         //将图片添加到图片视图
 33         imageView.image = image;
 34         //将图片视图添加到滚动视图上
 35         [scrollView addSubview:imageView];
 36 
 37         [imageView release];
 38     }
 39     [self.view addSubview:scrollView];
 40     [scrollView release];
 41     
 42 //创建pageControl
 43     UIPageControl * pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(bounds.origin.x, 20, bounds.size.width,50)];
 44     //tag
 45     pageControl.tag = 101;
 46     //页面数目
 47     pageControl.numberOfPages = 5;
 48     //默认第一页被选中,如果选择其他页面可以设置currentpage
 49     pageControl.currentPage = 0;//默认为0
 50     //在只有一个page时候隐藏指示器
 51     pageControl.hidesForSinglePage = YES;
 52     [self.view addSubview:pageControl];
 53     [self.view bringSubviewToFront:pageControl];
 54     [pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
 55 }
 56 
 57 - (void)pageChange:(UIPageControl *)page
 58 {
 59     UIScrollView * scrollView = (UIScrollView *)[self.view viewWithTag:102];
 60     switch (page.currentPage)
 61     {
 62         case 0:
 63             scrollView.contentOffset = CGPointMake(320 * page.currentPage, 0);
 64             break;
 65         case 1:
 66             scrollView.contentOffset = CGPointMake(320 * page.currentPage, 0);
 67             break;
 68         case 2:
 69             scrollView.contentOffset = CGPointMake(320 * page.currentPage, 0);
 70             break;
 71         case 3:
 72             scrollView.contentOffset = CGPointMake(320 * page.currentPage, 0);
 73             break;
 74         case 4:
 75             scrollView.contentOffset = CGPointMake(320 * page.currentPage, 0);
 76             break;      
 77         default:
 78             break;
 79     }
 80 }
 81 
 82 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
 83 {
 84     UIPageControl * pageControl = (UIPageControl *)[self.view viewWithTag:101];
 85     CGRect bounds = self.view.frame;
 86     if (scrollView.contentOffset.x == bounds.size.width * 0)
 87     {
 88         pageControl.currentPage = 0;
 89     }
 90     else if (scrollView.contentOffset.x == bounds.size.width * 1)
 91     {
 92         pageControl.currentPage = 1;
 93     }
 94     else if (scrollView.contentOffset.x == bounds.size.width * 2)
 95     {
 96         pageControl.currentPage = 2;
 97     }
 98     else if (scrollView.contentOffset.x == bounds.size.width * 3)
 99     {
100         pageControl.currentPage = 3;
101     }
102     else if (scrollView.contentOffset.x == bounds.size.width * 4)
103     {
104         pageControl.currentPage = 4;
105     }
106 }

 

posted on 2013-08-18 16:50  完美的超哥  阅读(563)  评论(0编辑  收藏  举报