UIPageControl页面控制的控件

#import "ViewController.h"

#import "LazyScrollView.h"

 

@interface ViewController ()<UIScrollViewDelegate>

{

    UIPageControl *pageControl;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    UIScrollView *scroView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    scroView.contentSize = CGSizeMake(CGRectGetWidth([UIScreen mainScreen] .bounds)*10, 0);

    scroView.delegate = self;// ?

    [self.view addSubview:scroView];

    

    CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds);

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

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(width*i, 0, width, CGRectGetHeight([UIScreen mainScreen].bounds))];

        view.backgroundColor = [UIColor purpleColor];

        view.layer.borderWidth = 4;// ?

        view.alpha = 0.7;

        view.layer.borderColor = [UIColor whiteColor].CGColor;

        [scroView addSubview:view];

    }

//  UIPageControl页面控制的控件

    /*

     可以通过UIPageControl 来确定  当前视图在哪一页

     1、一共有多少页  numberOfPages

     2、当前页面   currentPage

     

     UIPageControl ->.....

     

     */

    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 300, CGRectGetWidth([UIScreen mainScreen].bounds), 30)];

 

    //总共有多少页

    pageControl.numberOfPages = 10;

    //默认的当前页面

    pageControl.currentPage = 0;

    //设置页面控制的轨道颜色

    pageControl.pageIndicatorTintColor = [UIColor brownColor];

    //设置当前页面 的颜色

    pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];

    //当一个页面的时候隐藏点点点

    pageControl.hidesForSinglePage = YES;

    

    [self.view addSubview:pageControl];

    

    //如果默认页面  不是第一个页面  可以通过contentOffset  去设置偏移到哪一个位置

    

    scroView.contentOffset = CGPointMake(CGRectGetWidth([UIScreen mainScreen].bounds)*pageControl.currentPage, 0);

}

 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    NSInteger curIndex = scrollView.contentOffset.x/CGRectGetWidth(scrollView.frame);

    NSLog(@"%ld",curIndex);

    pageControl.currentPage = curIndex;

    

    

}

 

 

 

 

原文地址:https://www.cnblogs.com/liuzhi20101016/p/5054652.html