口袋优选

TAPageControl.m 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // TAPageControl.m
  3. // TAPageControl
  4. //
  5. // Created by Tanguy Aladenise on 2015-01-21.
  6. // Copyright (c) 2015 Tanguy Aladenise. All rights reserved.
  7. //
  8. #import "TAPageControl.h"
  9. #import "TAAbstractDotView.h"
  10. #import "TAAnimatedDotView.h"
  11. #import "TADotView.h"
  12. /**
  13. * Default number of pages for initialization
  14. */
  15. static NSInteger const kDefaultNumberOfPages = 0;
  16. /**
  17. * Default current page for initialization
  18. */
  19. static NSInteger const kDefaultCurrentPage = 0;
  20. /**
  21. * Default setting for hide for single page feature. For initialization
  22. */
  23. static BOOL const kDefaultHideForSinglePage = NO;
  24. /**
  25. * Default setting for shouldResizeFromCenter. For initialiation
  26. */
  27. static BOOL const kDefaultShouldResizeFromCenter = YES;
  28. /**
  29. * Default spacing between dots
  30. */
  31. static NSInteger const kDefaultSpacingBetweenDots = 8;
  32. /**
  33. * Default dot size
  34. */
  35. static CGSize const kDefaultDotSize = {8, 8};
  36. @interface TAPageControl()
  37. /**
  38. * Array of dot views for reusability and touch events.
  39. */
  40. @property (strong, nonatomic) NSMutableArray *dots;
  41. @end
  42. @implementation TAPageControl
  43. #pragma mark - Lifecycle
  44. - (id)init
  45. {
  46. self = [super init];
  47. if (self) {
  48. [self initialization];
  49. }
  50. return self;
  51. }
  52. - (id)initWithFrame:(CGRect)frame
  53. {
  54. self = [super initWithFrame:frame];
  55. if (self) {
  56. [self initialization];
  57. }
  58. return self;
  59. }
  60. - (id)initWithCoder:(NSCoder *)aDecoder
  61. {
  62. self = [super initWithCoder:aDecoder];
  63. if (self) {
  64. [self initialization];
  65. }
  66. return self;
  67. }
  68. /**
  69. * Default setup when initiating control
  70. */
  71. - (void)initialization
  72. {
  73. self.dotViewClass = [TAAnimatedDotView class];
  74. self.spacingBetweenDots = kDefaultSpacingBetweenDots;
  75. self.numberOfPages = kDefaultNumberOfPages;
  76. self.currentPage = kDefaultCurrentPage;
  77. self.hidesForSinglePage = kDefaultHideForSinglePage;
  78. self.shouldResizeFromCenter = kDefaultShouldResizeFromCenter;
  79. }
  80. #pragma mark - Touch event
  81. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  82. {
  83. UITouch *touch = [touches anyObject];
  84. if (touch.view != self) {
  85. NSInteger index = [self.dots indexOfObject:touch.view];
  86. if ([self.delegate respondsToSelector:@selector(TAPageControl:didSelectPageAtIndex:)]) {
  87. [self.delegate TAPageControl:self didSelectPageAtIndex:index];
  88. }
  89. }
  90. }
  91. #pragma mark - Layout
  92. /**
  93. * Resizes and moves the receiver view so it just encloses its subviews.
  94. */
  95. - (void)sizeToFit
  96. {
  97. [self updateFrame:YES];
  98. }
  99. - (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
  100. {
  101. return CGSizeMake((self.dotSize.width + self.spacingBetweenDots) * pageCount - self.spacingBetweenDots , self.dotSize.height);
  102. }
  103. /**
  104. * Will update dots display and frame. Reuse existing views or instantiate one if required. Update their position in case frame changed.
  105. */
  106. - (void)updateDots
  107. {
  108. if (self.numberOfPages == 0) {
  109. return;
  110. }
  111. for (NSInteger i = 0; i < self.numberOfPages; i++) {
  112. UIView *dot;
  113. if (i < self.dots.count) {
  114. dot = [self.dots objectAtIndex:i];
  115. } else {
  116. dot = [self generateDotView];
  117. }
  118. [self updateDotFrame:dot atIndex:i];
  119. }
  120. [self changeActivity:YES atIndex:self.currentPage];
  121. [self hideForSinglePage];
  122. }
  123. /**
  124. * Update frame control to fit current number of pages. It will apply required size if authorize and required.
  125. *
  126. * @param overrideExistingFrame BOOL to allow frame to be overriden. Meaning the required size will be apply no mattter what.
  127. */
  128. - (void)updateFrame:(BOOL)overrideExistingFrame
  129. {
  130. CGPoint center = self.center;
  131. CGSize requiredSize = [self sizeForNumberOfPages:self.numberOfPages];
  132. // We apply requiredSize only if authorize to and necessary
  133. if (overrideExistingFrame || ((CGRectGetWidth(self.frame) < requiredSize.width || CGRectGetHeight(self.frame) < requiredSize.height) && !overrideExistingFrame)) {
  134. self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), requiredSize.width, requiredSize.height);
  135. if (self.shouldResizeFromCenter) {
  136. self.center = center;
  137. }
  138. }
  139. [self resetDotViews];
  140. }
  141. /**
  142. * Update the frame of a specific dot at a specific index
  143. *
  144. * @param dot Dot view
  145. * @param index Page index of dot
  146. */
  147. - (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index
  148. {
  149. // Dots are always centered within view
  150. CGFloat x = (self.dotSize.width + self.spacingBetweenDots) * index + ( (CGRectGetWidth(self.frame) - [self sizeForNumberOfPages:self.numberOfPages].width) / 2);
  151. CGFloat y = (CGRectGetHeight(self.frame) - self.dotSize.height) / 2;
  152. dot.frame = CGRectMake(x, y, self.dotSize.width, self.dotSize.height);
  153. }
  154. #pragma mark - Utils
  155. /**
  156. * Generate a dot view and add it to the collection
  157. *
  158. * @return The UIView object representing a dot
  159. */
  160. - (UIView *)generateDotView
  161. {
  162. UIView *dotView;
  163. if (self.dotViewClass) {
  164. dotView = [[self.dotViewClass alloc] initWithFrame:CGRectMake(0, 0, self.dotSize.width, self.dotSize.height)];
  165. if ([dotView isKindOfClass:[TAAnimatedDotView class]] && self.dotColor) {
  166. ((TAAnimatedDotView *)dotView).dotColor = self.dotColor;
  167. }
  168. } else {
  169. dotView = [[UIImageView alloc] initWithImage:self.dotImage];
  170. dotView.frame = CGRectMake(0, 0, self.dotSize.width, self.dotSize.height);
  171. }
  172. if (dotView) {
  173. [self addSubview:dotView];
  174. [self.dots addObject:dotView];
  175. }
  176. dotView.userInteractionEnabled = YES;
  177. return dotView;
  178. }
  179. /**
  180. * Change activity state of a dot view. Current/not currrent.
  181. *
  182. * @param active Active state to apply
  183. * @param index Index of dot for state update
  184. */
  185. - (void)changeActivity:(BOOL)active atIndex:(NSInteger)index
  186. {
  187. if (self.dotViewClass) {
  188. TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[self.dots objectAtIndex:index];
  189. if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
  190. [abstractDotView changeActivityState:active];
  191. } else {
  192. NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
  193. }
  194. } else if (self.dotImage && self.currentDotImage) {
  195. UIImageView *dotView = (UIImageView *)[self.dots objectAtIndex:index];
  196. dotView.image = (active) ? self.currentDotImage : self.dotImage;
  197. }
  198. }
  199. - (void)resetDotViews
  200. {
  201. for (UIView *dotView in self.dots) {
  202. [dotView removeFromSuperview];
  203. }
  204. [self.dots removeAllObjects];
  205. [self updateDots];
  206. }
  207. - (void)hideForSinglePage
  208. {
  209. if (self.dots.count == 1 && self.hidesForSinglePage) {
  210. self.hidden = YES;
  211. } else {
  212. self.hidden = NO;
  213. }
  214. }
  215. #pragma mark - Setters
  216. - (void)setNumberOfPages:(NSInteger)numberOfPages
  217. {
  218. _numberOfPages = numberOfPages;
  219. // Update dot position to fit new number of pages
  220. [self resetDotViews];
  221. }
  222. - (void)setSpacingBetweenDots:(NSInteger)spacingBetweenDots
  223. {
  224. _spacingBetweenDots = spacingBetweenDots;
  225. [self resetDotViews];
  226. }
  227. - (void)setCurrentPage:(NSInteger)currentPage
  228. {
  229. // If no pages, no current page to treat.
  230. if (self.numberOfPages == 0 || currentPage == _currentPage) {
  231. _currentPage = currentPage;
  232. return;
  233. }
  234. // Pre set
  235. [self changeActivity:NO atIndex:_currentPage];
  236. _currentPage = currentPage;
  237. // Post set
  238. [self changeActivity:YES atIndex:_currentPage];
  239. }
  240. - (void)setDotImage:(UIImage *)dotImage
  241. {
  242. _dotImage = dotImage;
  243. [self resetDotViews];
  244. self.dotViewClass = nil;
  245. }
  246. - (void)setCurrentDotImage:(UIImage *)currentDotimage
  247. {
  248. _currentDotImage = currentDotimage;
  249. [self resetDotViews];
  250. self.dotViewClass = nil;
  251. }
  252. - (void)setDotViewClass:(Class)dotViewClass
  253. {
  254. _dotViewClass = dotViewClass;
  255. self.dotSize = CGSizeZero;
  256. [self resetDotViews];
  257. }
  258. #pragma mark - Getters
  259. - (NSMutableArray *)dots
  260. {
  261. if (!_dots) {
  262. _dots = [[NSMutableArray alloc] init];
  263. }
  264. return _dots;
  265. }
  266. - (CGSize)dotSize
  267. {
  268. // Dot size logic depending on the source of the dot view
  269. if (self.dotImage && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
  270. _dotSize = self.dotImage.size;
  271. } else if (self.dotViewClass && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
  272. _dotSize = kDefaultDotSize;
  273. return _dotSize;
  274. }
  275. return _dotSize;
  276. }
  277. @end