13-常见UI控件之 UIPageControl分页

UIPageControl

1.   numberOfPages // 设置有多少页 默认为0

// 2) 设置页数
   [pageControl setNumberOfPages: kImageCount];

2.   currentPage  // 设置当前页

[pageControl setCurrentPage: 0];
 

3.   pageIndicatorTintColor // 设置页码指示器颜色

 [pageControl setPageIndicatorTintColor:[UIColor blackColor]];

4.   currentPageIndicatorTintColor // 设置当前页码指示器颜色

 
[pageControl setCurrentPageIndicatorTintColor:[ UIColor  redColor]];
 
5. 添加分页控件的监听事件(监听值改变事件)
[pageControl addTarget: self  action: @selector(pageChanged:) forControlEvents: UIControlEventValueChanged];
 


//    创建分页控件
    pageControl=[[ UIPageControl alloc] initWithFrame: CGRectMake( 0, 250- 10, 375, 20)];
   
    pageControl. numberOfPages=imageNames. count;
   
    pageControl. currentPage= 0;
   
    [ pageControl addTarget: self action: @selector(pageAction:) forControlEvents: UIControlEventValueChanged];

    [headerView addSubview: pageControl];


-( void)pageAction:( UIPageControl*)pageControl{
   
    int page=pageControl. currentPage;
   
//    设置偏移量
//    [_scrollView setContentOffset:CGPointMake(page*375, 0) animated:YES];
   
//    滚动到某一个矩形区域,横向需要设置 x 偏移量,竖向设置 y
    [ _scrollView scrollRectToVisible: CGRectMake(page* 375, 0, 375, 250) animated: YES];
}
 

// 可以判断此协议,结束减速后在修改 pageControl.currentPage
- ( void)scrollViewDidEndDecelerating:( UIScrollView *)scrollView{
    if ( _scrollView==scrollView) {
       
         pageControl. currentPage = scrollView. contentOffset. x/ 375;
       
        currentPage;
    }

}

图片轮播器


@interface MJViewController () < UIScrollViewDelegate>
@property ( weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property ( weak, nonatomic) IBOutlet UIPageControl *pageControl;
/**
 * 
定时器
 */

@property ( nonatomic, strong) NSTimer *timer;
@end

@implementation MJViewController

- ( void)viewDidLoad
{
    [ super viewDidLoad];
   
    // 0. 一些固定的尺寸参数
    CGFloat imageW = self. scrollView. frame. size. width;
    CGFloat imageH = self. scrollView. frame. size. height;
    CGFloat imageY = 0;
   
    // 1. 添加 5 张图片到 scrollView
    for ( int i = 0; i< MJImageCount; i++) {
        UIImageView *imageView = [[ UIImageView alloc] init];
       
        // 设置 frame
        CGFloat imageX = i * imageW;
        imageView. frame = CGRectMake(imageX, imageY, imageW, imageH);
       
        // 设置图片
        NSString *name = [ NSString stringWithFormat: @"img_0%d", i + 1];
        imageView. image = [ UIImage imageNamed:name];
       
        [ self. scrollView addSubview:imageView];
    }
   
    // 2. 设置内容尺寸
    CGFloat contentW = MJImageCount * imageW;
    self. scrollView. contentSize = CGSizeMake(contentW, 0);
   
    // 3. 隐藏水平滚动条
    self. scrollView. showsHorizontalScrollIndicator = NO;
   
    // 4. 分页
    self. scrollView. pagingEnabled = YES;
//    self.scrollView.delegate = self;
   
    // 5. 设置 pageControl 的总页数
    self. pageControl. numberOfPages = MJImageCount;
   
    // 6. 添加定时器 ( 每隔 2 秒调用一次 self nextImage 方法 )
    [ self addTimer];
}

/**
 * 
添加定时器
 */

- ( void)addTimer
{
    self. timer = [ NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector: @selector(nextImage) userInfo: nil repeats: YES];
    [[ NSRunLoop currentRunLoop] addTimer: self. timer forMode: NSRunLoopCommonModes];
}

/**
 * 
移除定时器
 */

- ( void)removeTimer
{
    [ self. timer invalidate];
    self. timer = nil;
}

- ( void)nextImage
{
    // 1. 增加 pageControl 的页码
    int page = 0;
    if ( self. pageControl. currentPage == MJImageCount - 1) {
        page = 0;
    } else {
        page = self. pageControl. currentPage + 1;
    }
   
    // 2. 计算 scrollView 滚动的位置
    CGFloat offsetX = page * self. scrollView. frame. size. width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [ self. scrollView setContentOffset:offset animated: YES];
}

#pragma mark - 代理方法
/**
 * 
scrollView 正在滚动就会调用
 */

- ( void)scrollViewDidScroll:( UIScrollView *)scrollView
{
    // 根据 scrollView 的滚动位置决定 pageControl 显示第几页
    CGFloat scrollW = scrollView. frame. size. width;
    int page = (scrollView. contentOffset. x + scrollW * 0.5) / scrollW;
    self. pageControl. currentPage = page;
}

/**
 * 
开始拖拽的时候调用
 */

- ( void)scrollViewWillBeginDragging:( UIScrollView *)scrollView
{
    // 停止定时器 ( 一旦定时器停止了 , 就不能再使用 )
    [ self removeTimer];
}

/**
 * 
停止拖拽的时候调用
 */

- ( void)scrollViewDidEndDragging:( UIScrollView *)scrollView willDecelerate:( BOOL)decelerate
{
    // 开启定时器
    [ self addTimer];
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值