iOS学习笔记---UIPageControl的简单使用(用于分页)

无论在iPhone上还是其他设备上都有图片轮播,下方往往有那么几个点,表示显示图片的状态,也可以供我们选择:看一再在iOS中的简单使用:

UIPageControl在使用的时候是和其他控件联合使用的,尤其在和UIScrollView配合来显示较多的数据,会使用它来控制UIScrollView的翻页效果。


@property(nonatomic) NSInteger numberOfPages;          // default is 0  页数的总数
@property(nonatomic) NSInteger currentPage;            // default is 0. value pinned to 0..numberOfPages-1       当前的页数

@property(nonatomic) BOOL hidesForSinglePage;          // hide the the indicator if there is only one page. default is NO   如果只有一页,隐藏指示符

@property(nonatomic) BOOL defersCurrentPageDisplay;    // if set, clicking to a new page won't update the currently displayed page until -updateCurrentPageDisplay is called. default is NO

- (void)updateCurrentPageDisplay;                      // update page display to match the currentPage. ignored if defersCurrentPageDisplay is NO. setting the page value directly will update immediately

- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount;   // returns minimum size required to display dots for given page count. can be used to size control if page count could change 根据页数返回其尺寸

@property(nonatomic,retain) UIColor *pageIndicatorTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;//除了但前页面,其他页面的指示符颜色
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;  //当前页面的指示符的颜色

一个在分页中的使用事例(其getter方法):


- (UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        // 分页控件,本质上和scrollView没有任何关系,是两个独立的控件
        _pageControl = [[UIPageControl alloc] init];
        // 总页数
        _pageControl.numberOfPages = kImageCount;
        // 控件尺寸
        CGSize size = [_pageControl sizeForNumberOfPages:kImageCount];
        
        _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
        _pageControl.center = CGPointMake(self.view.center.x, 130);
        
        // 设置颜色
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        
        [self.view addSubview:_pageControl];
        
        // 添加监听方法
        /** 在OC中,绝大多数"控件",都可以监听UIControlEventValueChanged事件,button除外" */
        [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

当然要 UIScrollView在分页状态,初始化初始页( currentPage=0),当然还要 有定时器(NSTimer),还要保证页面转换的时候改变 currentPage

你可能感兴趣的:(ios)