UIPageControl基本用法

18 篇文章 0 订阅

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反复变化:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值