自定义UIPageControl圆点的图片

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

复制代码
GrayPageControl.h:


#import <Foundation/Foundation.h>



@interface GrayPageControl : UIPageControl

{

    UIImage* activeImage;

    UIImage* inactiveImage;

}


@end



GrayPageControl.m:
#import "GrayPageControl.h"



@implementation GrayPageControl

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

        CGSize size;

        size.height = 7;     //自定义圆点的大小

      size.width = 7;      //自定义圆点的大小
      [dot setFrame:CGRectMake(dot.frame.origin.x, dot.frame.origin.y, size.width, size.width)];
      if (i==self.currentPage)dot.image=activeImage;

      else dot.image=inactiveImage;

    }

}

-(void) setCurrentPage:(NSInteger)page

{

    [super setCurrentPage:page];

    [self updateDots];

}

@end
复制代码

试用该类的方法是:
pageControl = [[GrayPageControl alloc] initWithFrame:CGRectMake(0.0, 460.0 - (96 + 48) / 2, 320.0, 48.0 /2)];

pageControl.userInteractionEnabled = NO;

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

原文地址:https://www.cnblogs.com/worldtraveler/p/4596758.html