-
Notifications
You must be signed in to change notification settings - Fork 8
/
TAPageControl.m
executable file
·364 lines (277 loc) · 8.43 KB
/
TAPageControl.m
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// TAPageControl.m
// TAPageControl
//
// Created by Tanguy Aladenise on 2015-01-21.
// Copyright (c) 2015 Tanguy Aladenise. All rights reserved.
//
#import "TAPageControl.h"
#import "TAAbstractDotView.h"
#import "TAAnimatedDotView.h"
#import "TADotView.h"
/**
* Default number of pages for initialization
*/
static NSInteger const kDefaultNumberOfPages = 0;
/**
* Default current page for initialization
*/
static NSInteger const kDefaultCurrentPage = 0;
/**
* Default setting for hide for single page feature. For initialization
*/
static BOOL const kDefaultHideForSinglePage = NO;
/**
* Default setting for shouldResizeFromCenter. For initialiation
*/
static BOOL const kDefaultShouldResizeFromCenter = YES;
/**
* Default spacing between dots
*/
static NSInteger const kDefaultSpacingBetweenDots = 8;
/**
* Default dot size
*/
static CGSize const kDefaultDotSize = {8, 8};
@interface TAPageControl()
/**
* Array of dot views for reusability and touch events.
*/
@property (strong, nonatomic) NSMutableArray *dots;
@end
@implementation TAPageControl
#pragma mark - Lifecycle
- (id)init
{
self = [super init];
if (self) {
[self initialization];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialization];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialization];
}
return self;
}
/**
* Default setup when initiating control
*/
- (void)initialization
{
self.dotViewClass = [TAAnimatedDotView class];
self.spacingBetweenDots = kDefaultSpacingBetweenDots;
self.numberOfPages = kDefaultNumberOfPages;
self.currentPage = kDefaultCurrentPage;
self.hidesForSinglePage = kDefaultHideForSinglePage;
self.shouldResizeFromCenter = kDefaultShouldResizeFromCenter;
}
#pragma mark - Touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (touch.view != self) {
NSInteger index = [self.dots indexOfObject:touch.view];
if ([self.delegate respondsToSelector:@selector(TAPageControl:didSelectPageAtIndex:)]) {
[self.delegate TAPageControl:self didSelectPageAtIndex:index];
}
}
}
#pragma mark - Layout
/**
* Resizes and moves the receiver view so it just encloses its subviews.
*/
- (void)sizeToFit
{
[self updateFrame:YES];
}
- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
{
return CGSizeMake((self.dotSize.width + self.spacingBetweenDots) * pageCount - self.spacingBetweenDots , self.dotSize.height);
}
/**
* Will update dots display and frame. Reuse existing views or instantiate one if required. Update their position in case frame changed.
*/
- (void)updateDots
{
if (self.numberOfPages == 0) {
return;
}
for (NSInteger i = 0; i < self.numberOfPages; i++) {
UIView *dot;
if (i < self.dots.count) {
dot = [self.dots objectAtIndex:i];
} else {
dot = [self generateDotView];
}
[self updateDotFrame:dot atIndex:i];
}
[self changeActivity:YES atIndex:self.currentPage];
[self hideForSinglePage];
}
/**
* Update frame control to fit current number of pages. It will apply required size if authorize and required.
*
* @param overrideExistingFrame BOOL to allow frame to be overriden. Meaning the required size will be apply no mattter what.
*/
- (void)updateFrame:(BOOL)overrideExistingFrame
{
CGPoint center = self.center;
CGSize requiredSize = [self sizeForNumberOfPages:self.numberOfPages];
// We apply requiredSize only if authorize to and necessary
if (overrideExistingFrame || ((CGRectGetWidth(self.frame) < requiredSize.width || CGRectGetHeight(self.frame) < requiredSize.height) && !overrideExistingFrame)) {
self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), requiredSize.width, requiredSize.height);
if (self.shouldResizeFromCenter) {
self.center = center;
}
}
[self resetDotViews];
}
/**
* Update the frame of a specific dot at a specific index
*
* @param dot Dot view
* @param index Page index of dot
*/
- (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index
{
// Dots are always centered within view
CGFloat x = (self.dotSize.width + self.spacingBetweenDots) * index + ( (CGRectGetWidth(self.frame) - [self sizeForNumberOfPages:self.numberOfPages].width) / 2);
CGFloat y = (CGRectGetHeight(self.frame) - self.dotSize.height) / 2;
dot.frame = CGRectMake(x, y, self.dotSize.width, self.dotSize.height);
}
#pragma mark - Utils
/**
* Generate a dot view and add it to the collection
*
* @return The UIView object representing a dot
*/
- (UIView *)generateDotView
{
UIView *dotView;
if (self.dotViewClass) {
dotView = [[self.dotViewClass alloc] initWithFrame:CGRectMake(0, 0, self.dotSize.width, self.dotSize.height)];
if ([dotView isKindOfClass:[TAAnimatedDotView class]] && self.dotColor) {
((TAAnimatedDotView *)dotView).dotColor = self.dotColor;
}
} else {
dotView = [[UIImageView alloc] initWithImage:self.dotImage];
dotView.frame = CGRectMake(0, 0, self.dotSize.width, self.dotSize.height);
}
if (dotView) {
[self addSubview:dotView];
[self.dots addObject:dotView];
}
dotView.userInteractionEnabled = YES;
return dotView;
}
/**
* Change activity state of a dot view. Current/not currrent.
*
* @param active Active state to apply
* @param index Index of dot for state update
*/
- (void)changeActivity:(BOOL)active atIndex:(NSInteger)index
{
if (self.dotViewClass) {
TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[self.dots objectAtIndex:index];
if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
[abstractDotView changeActivityState:active];
} else {
NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
}
} else if (self.dotImage && self.currentDotImage) {
UIImageView *dotView = (UIImageView *)[self.dots objectAtIndex:index];
dotView.image = (active) ? self.currentDotImage : self.dotImage;
}
}
- (void)resetDotViews
{
for (UIView *dotView in self.dots) {
[dotView removeFromSuperview];
}
[self.dots removeAllObjects];
[self updateDots];
}
- (void)hideForSinglePage
{
if (self.dots.count == 1 && self.hidesForSinglePage) {
self.hidden = YES;
} else {
self.hidden = NO;
}
}
#pragma mark - Setters
- (void)setNumberOfPages:(NSInteger)numberOfPages
{
_numberOfPages = numberOfPages;
// Update dot position to fit new number of pages
[self resetDotViews];
}
- (void)setSpacingBetweenDots:(NSInteger)spacingBetweenDots
{
_spacingBetweenDots = spacingBetweenDots;
[self resetDotViews];
}
- (void)setCurrentPage:(NSInteger)currentPage
{
// If no pages, no current page to treat.
if (self.numberOfPages == 0 || currentPage == _currentPage) {
_currentPage = currentPage;
return;
}
// Pre set
[self changeActivity:NO atIndex:_currentPage];
_currentPage = currentPage;
// Post set
[self changeActivity:YES atIndex:_currentPage];
}
- (void)setDotImage:(UIImage *)dotImage
{
_dotImage = dotImage;
[self resetDotViews];
self.dotViewClass = nil;
}
- (void)setCurrentDotImage:(UIImage *)currentDotimage
{
_currentDotImage = currentDotimage;
[self resetDotViews];
self.dotViewClass = nil;
}
- (void)setDotViewClass:(Class)dotViewClass
{
_dotViewClass = dotViewClass;
self.dotSize = CGSizeZero;
[self resetDotViews];
}
#pragma mark - Getters
- (NSMutableArray *)dots
{
if (!_dots) {
_dots = [[NSMutableArray alloc] init];
}
return _dots;
}
- (CGSize)dotSize
{
// Dot size logic depending on the source of the dot view
if (self.dotImage && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
_dotSize = self.dotImage.size;
} else if (self.dotViewClass && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
_dotSize = kDefaultDotSize;
return _dotSize;
}
return _dotSize;
}
@end