UIPageControl的自定义小点颜色

来源:互联网 发布:js获取文本框内容 编辑:程序博客网 时间:2024/05/16 05:15

新建一个类,继承UIPageControl类:

#import <UIKit/UIKit.h>@interface UIBlackPageControl : UIPageControl    UIImage* activeImage;    UIImage* inactiveImage;@end

在.m文件中定义初始化函数:

- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self)    {        // Initialization code        activeImage = [[UIImage imageNamed:@"novice_dot_press@2x.png"] retain];        inactiveImage = [[UIImage imageNamed:@"novice_dot_nor@2x.png"] retain];        [self setCurrentPage:1];    }    return self;}

然后再将下面两个函数copy到你的.m文件中:

- (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];}



搞定!


使用的时候只需要将这个类的头文件包含进来,使用方式与UIPageControl完全相同。


}


原创粉丝点击