当需要改变UIPageControl的小圆点图片的时候,需要继承UIPageConrtol并重写几个方法。
设置好两个状态对应的两张图片,之后再写一个更新函数,再重写setCurrentPage函数
//
// MyUIPageControl.h
// MicroCourse
//
// Created by kay_sprint on 12-11-29.
// Copyright (c) 2012年 cn.edu.scnu. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MyUIPageControl : UIPageControl
@property (strong, nonatomic) UIImage *normalImage;
@property (strong, nonatomic) UIImage *highlightedImage;
@end
//
// MyUIPageControl.m
// MicroCourse
//
// Created by kay_sprint on 12-11-29.
// Copyright (c) 2012年 cn.edu.scnu. All rights reserved.
//
#import "MyUIPageControl.h"
@interface MyUIPageControl()
- (void)uodateDots;
@end
@implementation MyUIPageControl
@synthesize normalImage = _normalImage;
@synthesize highlightedImage = _highlightedImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.normalImage = [UIImage imageNamed:@"roll@2x.png"];
self.highlightedImage = [UIImage imageNamed:@"roll_highlight@2x.png"];
}
return self;
}
- (void)setNormalImage:(UIImage *)normalImage
{
_normalImage = normalImage;
[self updateDots];
}
- (void)setHighlightedImage:(UIImage *)highlightedImage
{
_highlightedImage = highlightedImage;
[self updateDots];
}
- (void)updateDots
{
if(_normalImage || _highlightedImage)
{
NSArray *subviews = self.subviews;
for(NSInteger i = 0;i<subviews.count;i++)
{
UIImageView *dot = [subviews objectAtIndex:i];
dot.image = self.currentPage == i ? _highlightedImage : _normalImage ;
}
}
}
- (void)setCurrentPage:(NSInteger)currentPage
{
[super setCurrentPage:currentPage];
[self updateDots];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
用的时候很简单 下面这样初始化一下就ok了
- (void)initPageControl
{
//加载自定义UIPageControl
_pageControl = [[MyUIPageControl alloc] initWithFrame:CGRectMake(104, 370, 300, 50)];
_pageControl.userInteractionEnabled = NO;
_pageControl.backgroundColor = [UIColor clearColor];
_pageControl.currentPage = 0;
_pageControl.numberOfPages = self.recCourseImgPath.count;
[self.view addSubview:_pageControl];
[_pageControl setCurrentPage:0];
}