您的位置:首页 > 产品设计 > UI/UE

UIPageControl基本用法

2015-12-01 14:24 666 查看
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反复变化:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIPageControl