iphone开发--改变UIPageControl里的小点的颜色

来源:互联网 发布:陕西广电网络秦岭云 编辑:程序博客网 时间:2024/05/18 01:16

iphone的UIPageControl控件可以显示用户huan'dong滑动到的页码。但是里面的小点的颜色时默认的白色。如果背景也是白色的hu话,你就悲剧了。于是乎上网找了一些资料,找到了改变UIPageControl空间xiao'da小点颜色的方法。解决fang'r方法如下:

GrayPageControl.h:

#import <Foundation/Foundation.h>

@interface GrayPageControl : UIPageControl

{

    UIImageactiveImage;

    UIImageinactiveImage;

}

@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 allocinitWithFrame:CGRectMake(0.0460.0 - (96 + 48) / 2320.048.0 /2)];

pageControl.userInteractionEnabled = NO;


注意:小圆点颜色改变时要调用pageControl中的setCurrentPage方法。
本人理解的思路:
首先GrayPageControl重载了UIPageControl的-(id) initWithFrame:(CGRect)frame方法。初始化了两个图片,即我们想要改变的小点点的颜色(一个是当前页的颜色,一个是非当前页的颜色)。
之后重载了UIPageControl的-(void) setCurrentPage:(NSInteger)page方法(此方法设置当前页的小点点的颜色)。注意在此处我们显式调用了-(void) updateDots方法,此方法中首先便利UIPageControl的子类,即每个小点点的UIImageView,我们设置每个小点点的imageView就可以了。哈哈。

上述代码转载自:http://stackoverflow.com/questions/3409319/is-there-a-way-to-change-page-indicator-dots-color
经过本人的一点小的修改,哈。
原创粉丝点击