//
// ViewController.m
// UIPageControl
//
// Created by City--Online on 15/5/19.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong) UIPageControl *pageControl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_pageControl=[[UIPageControl alloc]init];
_pageControl.frame=CGRectMake(20, 100, 100, 100);
_pageControl.backgroundColor=[UIColor grayColor];
//设置总页数
_pageControl.numberOfPages=5;
//设置当前页
_pageControl.currentPage=2;
//只有1页时隐藏
_pageControl.hidesForSinglePage=YES;
//根据页数获取需要的最小的size
CGSize size=[_pageControl sizeForNumberOfPages:5];
_pageControl.frame=CGRectMake(20, 100, size.width, size.height);
NSLog(@"%@",NSStringFromCGSize(size));
//设置圆点的颜色
_pageControl.pageIndicatorTintColor=[UIColor blueColor];
//设置当前选中圆点的颜色
_pageControl.currentPageIndicatorTintColor=[UIColor yellowColor];
//这个属性如果设置为YES,点击时并不会改变控制器显示的当前页码点,必须手动调用- (void)updateCurrentPageDisplay;这个方法,才会更新。
_pageControl.defersCurrentPageDisplay=YES;
[_pageControl addTarget:self action:@selector(pageIndexChanged) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
}
-(void)pageIndexChanged
{
NSInteger index=_pageControl.currentPage;
NSLog(@"%ld",index);
//暂停5秒钟
sleep(5);
//改变显示的页面 defersCurrentPageDisplay=NO时,方法无效
[_pageControl updateCurrentPageDisplay];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
此控件经常与UIScrollView控件一起使用。