天天看点

ios学习之自定义UIPageControl小点的颜色

自定义UIPageControl小点的颜色

因为iOS6.0以上版本才开放了更改UIPageControl中小点颜色的API,所以在6.0以下版本无法直接使用。这就需要我们自己来定义一个。

首先导入两个封装好的文件,在这里我的文件叫做BluePageControl.h和BluePageControl.m,继承于UIPageControl,重写了以下的方法。

BluePageControl.h:

@interfaceQBluePageControl : UIPageControl

{

UIImage *_activeImage;(蓝色圆点图片)

UIImage *_inactiveImage;(白色圆点图片)

}

@end

BluePageControl.m:

#import"QBluePageControl.h"

@implementationQBluePageControl

- (id)initWithFrame:(CGRect)frame

{

self = [superinitWithFrame:frame];

_activeImage = [UIImageimageNamed:@"channel_pagecontrol_bluedot.png"];

_inactiveImage = [UIImageimageNamed:@"channel_pagecontrol_whitedot.png"];

returnself;

}

- (void)updateDots

{

for (inti = 0; i< [self.subviewscount]; i++) {

UIImageView* dot = [self.subviewsobjectAtIndex:i];

if (i == self.currentPage){

dot.image = _activeImage;

        }

else

dot.image = _inactiveImage;

    }

}

- (void)setCurrentPage:(NSInteger)currentPage

{

    [supersetCurrentPage:currentPage];

    [selfupdateDots];

}

@end

使用的时候在主程序中#import "QBluePageControl.h"

创建:_pageControl = [[QBluePageControlalloc] init];并指定_pageControl的numberOfPages和currentPage就可以实现了。

以上是我最近做大有应用时所遇到的小问题,以后会持续和大家分享我在开发上的点点滴滴。