1、我的理解,他就是一个指示作用,当然通过监视滑动动作,也可以控制页面、视图的变化,可以和UIScrollView、UIPageViewController等配合使用
基本用法如下:
//
// ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
UIPageControl * _pageControl;
BOOL isAdd;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
isAdd = true;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
_pageControl = [[UIPageControl alloc] init];
_pageControl.frame = CGRectMake(25, height - 100, width - 50, 30);
_pageControl.backgroundColor = [UIColor orangeColor];
//设置控制页面数,就是多少个点
_pageControl.numberOfPages = 6;
//设置当前点
_pageControl.currentPage = 3;
//设置所有点的颜色
_pageControl.pageIndicatorTintColor = [UIColor whiteColor];
//设置当前点得颜色
_pageControl.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:_pageControl];
//如果这个设置为YES,则当前点不会变
// _pageControl.defersCurrentPageDisplay = YES;
//添加监视其状态的事件
[_pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 50, 30);
btn.layer.cornerRadius = 10;
[btn setTitle:@"点击" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor purpleColor]];
}
- (void) pageControlClick : (id) sender {
NSLog(@"%li", (long)_pageControl.currentPage);
}
- (void) btnClick : (id) sender {
if (isAdd) {
_pageControl.currentPage += 1;
}else{
_pageControl.currentPage -= 1;
}
if (_pageControl.currentPage == 5) {
isAdd = false;
}
if (_pageControl.currentPage == 0) {
isAdd = true;
}
//更新UI
[_pageControl updateCurrentPageDisplay];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
连续点击button可以控制UIPageControl反复变化: