UIPageControl自定义点的颜色

来源:互联网 发布:淘宝企业采购 编辑:程序博客网 时间:2024/05/16 15:25

UIPageControl自定义点的颜色


 屏幕快照 2011-04-02 下午12.32.27 屏幕快照 2011-04-02 下午12.32.47 屏幕快照 2011-04-02 下午12.27.39 

首先导入已经封装好的两个文件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的对象做就可以了,另外注意,关联控件的时候屏幕快照 2011-04-02 下午03.17.27 要继承GrayPageControl类


原创粉丝点击