首先导入已经封装好的两个文件GrayPageControl.h,GrayPageControl.m,里面继承了UIPageControl,重写了他的方法。文件内容如下:
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:@"inactive_page_image.png"] retain];
inactiveImage = [[UIImage imageNamed:@"active_page_image.png"] retain];
[self setCurrentPage:1];
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];
}
-(void)dealloc
{
[super dealloc];
}
@end
在主程序中,只要程序把GrayPageControl的对象当做UIPageControl的对象做就可以了,另外注意,关联控件的时候 要继承GrayPageControl类