自义定UIPageControl
@interface ZHPageControl : UIPageControl{ UIImage *imagePageStateNormal; UIImage *imagePageStateHighlighted; } - (id)initWithFrame:(CGRect)frame; @property (nonatomic, retain) UIImage *imagePageStateNormal; @property (nonatomic, retain) UIImage *imagePageStateHighlighted; @end
#import "ZHPageControl.h" /** * @brief 声明一个私有方法, 该方法不允许对象直接使用 * * @param private <#private description#> * * @return <#return value description#> */ @interface ZHPageControl(private) -(void)updateDots; @end @implementation ZHPageControl @synthesize imagePageStateNormal; @synthesize imagePageStateHighlighted; /** * @brief 初始化 * * @param frame <#frame description#> * * @return <#return value description#> */ - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; return self; } /** * @brief 设置正常状态点按钮的图片 * * @param image <#image description#> */ - (void)setImagePageStateNormal:(UIImage *)image { [imagePageStateNormal release]; imagePageStateNormal = [image retain]; [self updateDots]; } /** * @brief 设置高亮状态点按钮图片 * * @param image <#image description#> */ - (void)setImagePageStateHighlighted:(UIImage *)image { [imagePageStateHighlighted release]; imagePageStateHighlighted = [image retain]; [self updateDots]; } /** * @brief 重写 setCurrentPage方法 * * @param currentPage 当前页 */ - (void)setCurrentPage:(NSInteger)currentPage { [super setCurrentPage:currentPage]; [self updateDots]; } /** * @brief 更新显示所有的点按钮 */ - (void)updateDots { if (imagePageStateNormal || imagePageStateHighlighted) { NSArray *subview = self.subviews; // 获取所有子视图 for (NSInteger i = 0; i < [subview count]; i++) { UIImageView *dot = [subview objectAtIndex:i]; // 以下不解释, 看了基本明白 dot.image = self.currentPage == i ? imagePageStateNormal : imagePageStateHighlighted; } } } /** * @brief 点击事件 * * @param touch <#touch description#> * @param event <#event description#> */ - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { [super endTrackingWithTouch:touch withEvent:event]; [self updateDots]; } /** * @brief 释放内存 */ - (void)dealloc { [imagePageStateNormal release], imagePageStateNormal = nil; [imagePageStateHighlighted release], imagePageStateHighlighted = nil; [super dealloc]; } /** * @brief 覆盖的drawRect:执行自定义绘制。 * * @param rect <#rect description#> */ - (void)drawRect:(CGRect)rect { int count = [self.subviews count]; int width=44;//自定义图标宽 int height=44;//自定义图标高 for (int i = 0; i < count; i++) { UIImageView* dot = [self.subviews objectAtIndex:i]; [dot setFrame:CGRectMake(i*width, 0, width, height)]; if (i == 0) { [dot setImage:imagePageStateHighlighted]; }else { [dot setImage:imagePageStateNormal]; } } } @end