您可以通过以下步骤来实现:
1- 创建一个继承自 UIPageControl 的自定义类。
2- 将此类分配给您要更改其点的所需 UIPageControl。
3- 将以下代码放入您的自定义 UIPageControl 类中。
把这个放到customClass.m文件中:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
activeImage = [UIImage imageNamed:@"active_dot.png"];
inactiveImage = [UIImage imageNamed:@"inactive_dot.png"];
}
return self;
}
-(void)updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* dot = [self.subviews objectAtIndex:i];
dot.frame = CGRectMake(dot.frame.origin.x, dot.frame.origin.y, 14, 14.5);
if (i == self.currentPage)
dot.image = activeImage;
else
dot.image = inactiveImage;
}
}
-(void)setCurrentPage:(NSInteger)page
{
[super setCurrentPage:page];
[self updateDots];
}
把这个放到customClass.h文件中
{
UIImage* activeImage;
UIImage* inactiveImage;
}
@property(nonatomic, retain) UIImage* activeImage;
@property(nonatomic, retain) UIImage* inactiveImage;
5- 只需将 UIPageControl 的当前页面设置在您将页面控件放入其中的类中,使用以下行:
[self.pageControl setCurrentPage:number];
记得在 viewDidLoad() 方法中设置当前页面在 UIPageControl 所在的视图中。
当视图加载时,UIPageControl 图像将被设置。