天天看點

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就可以實作了。

以上是我最近做大有應用時所遇到的小問題,以後會持續和大家分享我在開發上的點點滴滴。