1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// CustomPageControl.m
// Parking
//
// Created by admin on 15/9/16.
// Copyright (c) 2015年 moobox. All rights reserved.
//
#import "CustomPageControl.h"
#define kDotDiameter 4.0f
#define kDotSpace 12.0f
@implementation CustomPageControl
@synthesize numberOfPages ;
@synthesize currentPage ;
@synthesize hidesForSinglePage ;
@synthesize defersCurrentPageDisplay ;
@synthesize onColor ;
@synthesize offColor ;
@synthesize indicatorDiameter ;
@synthesize indicatorSpace ;
#pragma mark - init
- (id)init {
self = [self initWithFrame:CGRectZero] ;
return self ;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:CGRectZero])) {
self.backgroundColor = [UIColor clearColor] ;
}
return self ;
}
#pragma mark - drawRect
- (void)drawRect:(CGRect)rect {
// 获取上下文
CGContextRef context = UIGraphicsGetCurrentContext() ;
CGContextSaveGState(context) ;
CGContextSetAllowsAntialiasing(context, TRUE) ;
// 判断小点点直径 间距
CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter ;
CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace ;
// 整个pageControl的bounds
CGRect currentBounds = self.bounds;
CGFloat dotsWidth = self.numberOfPages * diameter + MAX(0, self.numberOfPages - 1) * space;
CGFloat x = CGRectGetMidX(currentBounds) - dotsWidth / 2;
CGFloat y = CGRectGetMidY(currentBounds) - diameter / 2;
// 设置选中和未选中的小点点的颜色
CGColorRef onColorCG = onColor ? onColor.CGColor : [UIColor colorWithWhite: 1.0f alpha: 1.0f].CGColor ;
CGColorRef offColorCG = offColor ? offColor.CGColor : [UIColor colorWithWhite: 0.7f alpha: 0.5f].CGColor ;
// 遍历画(draw)点
for (int i = 0 ; i < numberOfPages ; i++)
{
CGRect dotRect = CGRectMake(x, y, diameter, diameter) ;
if (i == currentPage) {
CGContextSetFillColorWithColor(context, onColorCG) ;
CGContextFillEllipseInRect(context, CGRectInset(dotRect, -0.5f, -0.5f)) ;
} else {
CGContextSetStrokeColorWithColor(context, offColorCG) ;
CGContextStrokeEllipseInRect(context, dotRect) ;
}
x += diameter + space ;
}
// 恢复画板
CGContextRestoreGState(context) ;
}
#pragma mark - 重写setter方法
- (void)setCurrentPage:(NSInteger)pageNumber {
if (currentPage == pageNumber) {
return;
}
currentPage = MIN(MAX(0, pageNumber), numberOfPages - 1);
if (self.defersCurrentPageDisplay == NO) {
[self setNeedsDisplay];
}
}
- (void)setNumberOfPages:(NSInteger)numOfPages {
numberOfPages = MAX(0, numOfPages);
currentPage = MIN(MAX(0, currentPage), numberOfPages - 1);
self.bounds = self.bounds;
[self setNeedsDisplay];
if (hidesForSinglePage && (numOfPages < 2)) {
[self setHidden: YES];
} else {
[self setHidden: NO];
}
}
- (void)setHidesForSinglePage:(BOOL)hide {
hidesForSinglePage = hide ;
if (hidesForSinglePage && (numberOfPages < 2)){
[self setHidden:YES];
}
}
- (void)setDefersCurrentPageDisplay:(BOOL)defers {
defersCurrentPageDisplay = defers ;
}
- (void)setOnColor:(UIColor *)aColor {
onColor = aColor;
[self setNeedsDisplay];
}
- (void)setOffColor:(UIColor *)aColor {
offColor = aColor;
[self setNeedsDisplay];
}
- (void)setIndicatorDiameter:(CGFloat)aDiameter {
indicatorDiameter = aDiameter;
self.bounds = self.bounds;
[self setNeedsDisplay];
}
- (void)setIndicatorSpace:(CGFloat)aSpace {
indicatorSpace = aSpace;
self.bounds = self.bounds;
[self setNeedsDisplay];
}
- (void)setFrame:(CGRect)aFrame {
aFrame.size = [self sizeForNumberOfPages:numberOfPages];
super.frame = aFrame;
}
- (void)setBounds:(CGRect)aBounds {
aBounds.size = [self sizeForNumberOfPages: numberOfPages];
super.bounds = aBounds;
}
#pragma mark UIPageControl methods
- (void)updateCurrentPageDisplay {
if (self.defersCurrentPageDisplay == NO) {
return;
}
[self setNeedsDisplay] ;
}
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount {
CGFloat diameter = (indicatorDiameter > 0) ? indicatorDiameter : kDotDiameter;
CGFloat space = (indicatorSpace > 0) ? indicatorSpace : kDotSpace;
return CGSizeMake(pageCount * diameter + (pageCount - 1) * space + 44.0f, MAX(44.0f, diameter + 4.0f));
}
#pragma mark - Touches handlers
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// 获取触摸的地方
UITouch *theTouch = [touches anyObject] ;
CGPoint touchLocation = [theTouch locationInView: self] ;
// 检测触摸的方向
if (touchLocation.x < (self.bounds.size.width / 2)) {
self.currentPage = MAX(self.currentPage - 1, 0);
} else {
self.currentPage = MIN(self.currentPage + 1, numberOfPages - 1);
}
[self sendActionsForControlEvents: UIControlEventValueChanged] ;
}
@end