[转载]iphone开发--改变UIPageControl里的小点的颜色
iphone開發--改變UIPageControl里的小點的顏色
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) initWithCoder:(NSCoder *)aDecoder
//{
//????self = [super initWithCoder:aDecoder];
//?
//????activeImage = [[UIImage imageNamed:@"RedPoint.png"] retain];
//????inactiveImage = [[UIImage imageNamed:@"BluePoint.png"] retain];
//?
//????return self;
//}
-(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];
????????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就可以了。哈哈。
總結
以上是生活随笔為你收集整理的[转载]iphone开发--改变UIPageControl里的小点的颜色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UITabBarController使用
- 下一篇: [Cocoa]深入浅出 Cocoa 之