IOS自定义UIPageControl

1.修改大小,创建一个类,继承UIPageControl,重写setCurrentPage方法,可设置圆点大小

- (void) setCurrentPage:(NSInteger)page {
    [super setCurrentPage:page];

    for (NSUInteger subviewIndex = 0; subviewIndex < [self.subviews count]; subviewIndex++) {
        UIImageView* subview = [self.subviews objectAtIndex:subviewIndex];
        CGSize size;
        size.height = 10;
        size.width = 10;
        [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,size.width,size.height)];
    }
}
3.将点显示成图片的方法
[pageCon setValue:[UIImage imageNamed:@"BluePoint.png"] forKeyPath:@"_currentPageImage"];
[pageCon setValue:[UIImage imageNamed:@"black"] forKeyPath:@"_pageImage"];
4.设置点的颜色的方法
pageCon.pageIndicatorTintColor = [UIColor blackColor];
pageCon.currentPageIndicatorTintColor = [UIColor redColor];

示例

   //创建初始化并设置PageControl
    SCPageControl *pagecontrol = [[SCPageControl alloc]init];
    pagecontrol.numberOfPages = 5; //因为有5张图片,所以设置分页数为5
    pagecontrol.currentPage  = 0; //默认第一页页数为0
    //设置分页控制点颜色
    pagecontrol.pageIndicatorTintColor = [UIColor redColor];//未选中的颜色
    pagecontrol.currentPageIndicatorTintColor = [UIColor greenColor];//选中时的颜色
    //添加分页控制事件用来分页
    //[self.pagecontrol addTarget:self action:@selector(pageControlChanged:) forControlEvents:UIControlEventValueChanged];
    //将分页控制视图添加到视图控制器视图中
    [self.view addSubview:pagecontrol];
    pagecontrol.frame=CGRectMake(10, 200,self.view.frame.size.width-20, 50);
    pagecontrol.backgroundColor=[UIColor yellowColor];
    
    [pagecontrol setValue:[UIImage imageNamed:@"index_sublunbo_black"] forKeyPath:@"_currentPageImage"];
    [pagecontrol setValue:[UIImage imageNamed:@"index_sublunbo_white"] forKeyPath:@"_pageImage"];

你可能感兴趣的:(IOS自定义UIPageControl)