iphone的UIPageControl控件可以顯示用戶huan'dong滑動到的頁碼。但是里面的小點的顏色時默認的白色。如果背景也是白色的hu話,你就悲劇了。於是乎上網找了一些資料,找到了改變UIPageControl空間xiao'da小點顏色的方法。解決fang'r方法如下:
GrayPageControl.h: #import <Foundation/Foundation.h> @interface GrayPageControl : UIPageControl { UIImage* activeImage; UIImage* inactiveImage; } @end GrayPageControl.m: #import "GrayPageControl.h" @implementation GrayPageControl -(id) initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; activeImage = [[UIImage imageNamed:@"RedPoint.png"] retain]; inactiveImage = [[UIImage imageNamed:@"BluePoint.png"] retain]; return self; } -(void) updateDots {
for (int i=0; i<[self.subviews count]; i++) {
UIImageView* dot = [self.subviews objectAtIndex:i];
CGSize size;
size.height = 7; //自定義圓點的大小
size.width = 7; //自定義圓點的大小
[dot setFrame:CGRectMake(dot.frame.origin.x, dot.frame.origin.y, size.width, size.width)];
if (i==self.currentPage)dot.image=activeImage;
else dot.image=inactiveImage;
}
}
-(void) setCurrentPage:(NSInteger)page { [super setCurrentPage:page]; [self updateDots]; } @end
試用該類的方法是:
pageControl = [[GrayPageControl alloc] initWithFrame:CGRectMake(0.0, 460.0 - (96 + 48) / 2, 320.0, 48.0 /2)];
pageControl.userInteractionEnabled = NO;
注意:小圓點顏色改變時要調用pageControl中的setCurrentPage方法。
本人理解的思路:
首先GrayPageControl重載了UIPageControl的-(id) initWithFrame:(CGRect)frame方法。初始化了兩個圖片,即我們想要改變的小點點的顏色(一個是當前頁的顏色,一個是非當前頁的顏色)。
之后重載了UIPageControl的-(void) setCurrentPage:(NSInteger)page方法(此方法設置當前頁的小點點的顏色)。注意在此處我們顯式調用了-(void) updateDots方法,此方法中首先便利UIPageControl的子類,即每個小點點的UIImageView,我們設置每個小點點的imageView就可以了。