借鑒文章:http://blog.csdn.net/showhilllee/article/details/8350663
找了好多文章,想找個(gè)最簡(jiǎn)單的方法來(lái)自定義pageControl,覺(jué)得這種方法是代碼相對(duì)較少的,但是帖子比較古老了,里面的 updateDots 設(shè)置的為UImageView,現(xiàn)在xcode是跑不起來(lái)的,dug下發(fā)現(xiàn)原來(lái),現(xiàn)在的subviews 中存的為UIView,直接上一段代碼,隨手寫的demo沒(méi)有像外界提供任何接口,純屬了解思路用
.h文件中
import <UIKit/UIKit.h>
@interface XMPageControl : UIPageControl
@end
.m文件
import "XMPageControl.h"
#define SCREEN_WITH [[UIScreen mainScreen]bounds].size.width
@implementation XMPageControl
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
return self;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super endTrackingWithTouch:touch withEvent:event];
[self updateDots];
}
- (void)updateDots {
for (int i=0; i<self.subviews.count; ++i) {
UIView *dot = [self.subviews objectAtIndex:i];
//此處兩個(gè)color可以抽出,為外界提供接口
dot.backgroundColor = i==self.currentPage ? [UIColor orangeColor]:[UIColor redColor];
}
}
/**
* 如果想實(shí)現(xiàn)帖子這種http://blog.csdn.net/showhilllee/article/details/8350663設(shè)置image,
* 可參考思路,在layoutSurviews中 remove掉所有的 view 替換為imageView,
* 還可設(shè)置uiview的layer根據(jù)image繪制 backgroundcolor
*/
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat space = 5.f; //space, dotW H 等可以可抽出為外界所用
CGFloat dotW = SCREEN_WITH/14.f;
CGFloat dotH = dotW/7.f;
CGFloat totalWith = (dotW+space) * self.numberOfPages;
for (int i=0; i<self.subviews.count; ++i) {
UIView *dot = [self.subviews objectAtIndex:i];
CGFloat dotX = (self.frame.size.width-totalWith)/2.f + (space+dotW)*i;
CGFloat dotY = (self.frame.size.height-dotH)/2.f;
dot.frame = CGRectMake(dotX, dotY, dotW, dotH);
}
}
- (void)setCurrentPage:(NSInteger)currentPage {
[super setCurrentPage:currentPage];
[self updateDots];
}
@end
沒(méi)有提供任何的接口,用法和常規(guī)無(wú)異!
效果如圖:

Snip20160523_2.png