UIPageControl 页码控制器

    //UIPageControl主要用来控制分页, 通常和UIScrollView一起使用

//    UIPageControl *firstPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

//    

//    

//    firstPageControl.backgroundColor = [UIColor orangeColor];

//    

//    [self.view addSubview:firstPageControl];

//    

//    [firstPageControl release];

//    

//    //设置页数

//    firstPageControl.numberOfPages = 5;

//    

//    //设置当前为第几页 属性: currentPage(默认 0(第一页))

//    firstPageControl.currentPage = 1;

//    

//    //设置除了当前点的其他点的背景颜色

//    firstPageControl.pageIndicatorTintColor = [UIColor greenColor];

//    

//    

//    //设置选中的点的背景颜色

//    firstPageControl.currentPageIndicatorTintColor = [UIColor blackColor];

//    

    

    

    

    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 200, self.view.frame.size.height - 150, 200, 50)];

    

    self.page.backgroundColor = [UIColor clearColor];

    

    self.page.currentPage = 1;

    

    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];

    

    self.page.pageIndicatorTintColor = [UIColor grayColor];

    

    

    self.page.numberOfPages = 4;

    

    //添加事件, 实现点击pageControl, 使ScrollView变换图片

    [self.page addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];

    

    

    

    [self.view addSubview:_page];

    

    [self.view bringSubviewToFront:_page];

    

    

    UIScrollView *images = [[UIScrollView alloc]initWithFrame:self.view.frame];

    

    images.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height);

    

    for (int i = 0; i < 4; i++) {

        

        UIImageView *picture = [[UIImageView alloc]initWithFrame:CGRectMake(images.frame.size.width * i, 0, images.frame.size.width, images.frame.size.height)];

        

        picture.image = [UIImage imageNamed:[NSString stringWithFormat:@"bigbang%d.jpg", i + 1]];

        

        [images addSubview:picture];

        

    }

    

    

    

    images.pagingEnabled = YES;

    

    images.delegate = self;

    

    images.tag = 100;

    

    [self.view addSubview:images];

    

    [self.view sendSubviewToBack:images];

    

    [images release];

    

    

}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(scrollView.frame.size.width - 150, scrollView.frame.size.height - 100, 100, 50)];

//    

//    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];

//    

//    pageControl.backgroundColor = [UIColor orangeColor];

//    

//    pageControl.numberOfPages = 4;

//    

//    int temp = scrollView.contentOffset.x / scrollView.frame.size.width;

//    

//    pageControl.currentPage = temp;

//    

//    pageControl.pageIndicatorTintColor = [UIColor grayColor];

//    

//    

//    [self.view addSubview:pageControl];

//    

//    [pageControl release];

    

    self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;

    

    //[self.view bringSubviewToFront:_page];

    

    

    

    

}



-(void)pageControlAction:(UIPageControl *)pageControl

{

    

    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];

    

    scrollView.contentOffset = CGPointMake(self.view.frame.size.width * pageControl.currentPage, 0);

    

    

    

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值