StyledPageControl.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // PageControl.m
  3. // PageControlDemo
  4. //
  5. /**
  6. * Created by honcheng on 5/14/11.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject
  14. * to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
  20. * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  21. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR
  23. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  24. * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  27. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  28. * IN CONNECTION WITH THE SOFTWARE OR
  29. * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. *
  31. * @author Muh Hon Cheng <honcheng@gmail.com> http://twitter.com/honcheng
  32. * @copyright 2011 Muh Hon Cheng
  33. *
  34. */
  35. #import "StyledPageControl.h"
  36. @implementation StyledPageControl
  37. @synthesize _numberOfPages, _currentPage, hidesForSinglePage;
  38. @synthesize coreNormalColor, coreSelectedColor;
  39. @synthesize strokeNormalColor, strokeSelectedColor;
  40. @synthesize _pageControlStyle, _strokeWidth, diameter, gapWidth;
  41. @synthesize thumbImage, selectedThumbImage;
  42. #define COLOR_GRAYISHBLUE [UIColor colorWithRed:128/255.0 green:130/255.0 blue:133/255.0 alpha:1]
  43. #define COLOR_GRAY [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]
  44. - (id)initWithFrame:(CGRect)frame
  45. {
  46. self = [super initWithFrame:frame];
  47. if (self) {
  48. // Initialization code
  49. [self setBackgroundColor:[UIColor clearColor]];
  50. self._strokeWidth = 2;
  51. self.gapWidth = 10;
  52. self.diameter = 12;
  53. self._pageControlStyle = PageControlStyleDefault;
  54. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapped:)];
  55. [self addGestureRecognizer:tapGestureRecognizer];
  56. [tapGestureRecognizer release];
  57. }
  58. return self;
  59. }
  60. - (void)onTapped:(UITapGestureRecognizer*)gesture
  61. {
  62. CGPoint touchPoint = [gesture locationInView:[gesture view]];
  63. if (touchPoint.x < self.frame.size.width/2)
  64. {
  65. // move left
  66. if (self._currentPage>0)
  67. {
  68. self._currentPage -= 1;
  69. }
  70. }
  71. else
  72. {
  73. // move right
  74. if (self._currentPage<self._numberOfPages-1)
  75. {
  76. self._currentPage += 1;
  77. }
  78. }
  79. [self setNeedsDisplay];
  80. [self sendActionsForControlEvents:UIControlEventValueChanged];
  81. }
  82. - (void)drawRect:(CGRect)rect
  83. {
  84. UIColor *_coreNormalColor, *_coreSelectedColor, *_strokeNormalColor, *_strokeSelectedColor;
  85. if (self.coreNormalColor) _coreNormalColor = self.coreNormalColor;
  86. else _coreNormalColor = COLOR_GRAYISHBLUE;
  87. if (self.coreSelectedColor) _coreSelectedColor = self.coreSelectedColor;
  88. else
  89. {
  90. if (self._pageControlStyle==PageControlStyleStrokedCircle || self._pageControlStyle==PageControlStyleWithPageNumber)
  91. {
  92. _coreSelectedColor = COLOR_GRAYISHBLUE;
  93. }
  94. else
  95. {
  96. _coreSelectedColor = COLOR_GRAY;
  97. }
  98. }
  99. if (self.strokeNormalColor) _strokeNormalColor = self.strokeNormalColor;
  100. else
  101. {
  102. if (self._pageControlStyle==PageControlStyleDefault && self.coreNormalColor)
  103. {
  104. _strokeNormalColor = self.coreNormalColor;
  105. }
  106. else
  107. {
  108. _strokeNormalColor = COLOR_GRAYISHBLUE;
  109. }
  110. }
  111. if (self.strokeSelectedColor) _strokeSelectedColor = self.strokeSelectedColor;
  112. else
  113. {
  114. if (self._pageControlStyle==PageControlStyleStrokedCircle || self._pageControlStyle==PageControlStyleWithPageNumber)
  115. {
  116. _strokeSelectedColor = COLOR_GRAYISHBLUE;
  117. }
  118. else if (self._pageControlStyle==PageControlStyleDefault && self.coreSelectedColor)
  119. {
  120. _strokeSelectedColor = self.coreSelectedColor;
  121. }
  122. else
  123. {
  124. _strokeSelectedColor = COLOR_GRAY;
  125. }
  126. }
  127. // Drawing code
  128. if (hidesForSinglePage && self._numberOfPages==1)
  129. {
  130. return;
  131. }
  132. CGContextRef myContext = UIGraphicsGetCurrentContext();
  133. int gap = self.gapWidth;
  134. float _diameter = self.diameter - 2*self._strokeWidth;
  135. if (self.pageControlStyle==PageControlStyleThumb)
  136. {
  137. if (self.thumbImage && self.selectedThumbImage)
  138. {
  139. _diameter = self.thumbImage.size.width;
  140. }
  141. }
  142. int total_width = self._numberOfPages*_diameter + (self._numberOfPages-1)*gap;
  143. if (total_width>self.frame.size.width)
  144. {
  145. while (total_width>self.frame.size.width)
  146. {
  147. _diameter -= 2;
  148. gap = _diameter + 2;
  149. while (total_width>self.frame.size.width)
  150. {
  151. gap -= 1;
  152. total_width = self._numberOfPages*_diameter + (self._numberOfPages-1)*gap;
  153. if (gap==2)
  154. {
  155. break;
  156. total_width = self._numberOfPages*_diameter + (self._numberOfPages-1)*gap;
  157. }
  158. }
  159. if (_diameter==2)
  160. {
  161. break;
  162. total_width = self._numberOfPages*_diameter + (self._numberOfPages-1)*gap;
  163. }
  164. }
  165. }
  166. int i;
  167. for (i=0; i<self._numberOfPages; i++)
  168. {
  169. int x = (self.frame.size.width-total_width)/2 + i*(_diameter+gap);
  170. if (self._pageControlStyle==PageControlStyleDefault)
  171. {
  172. if (i==_currentPage)
  173. {
  174. CGContextSetFillColorWithColor(myContext, [_coreSelectedColor CGColor]);
  175. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  176. CGContextSetStrokeColorWithColor(myContext, [_strokeSelectedColor CGColor]);
  177. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  178. }
  179. else
  180. {
  181. CGContextSetFillColorWithColor(myContext, [_coreNormalColor CGColor]);
  182. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  183. CGContextSetStrokeColorWithColor(myContext, [_strokeNormalColor CGColor]);
  184. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  185. }
  186. }
  187. else if (self._pageControlStyle==PageControlStyleStrokedCircle)
  188. {
  189. CGContextSetLineWidth(myContext, self._strokeWidth);
  190. if (i==_currentPage)
  191. {
  192. CGContextSetFillColorWithColor(myContext, [_coreSelectedColor CGColor]);
  193. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  194. CGContextSetStrokeColorWithColor(myContext, [_strokeSelectedColor CGColor]);
  195. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  196. }
  197. else
  198. {
  199. CGContextSetStrokeColorWithColor(myContext, [_strokeNormalColor CGColor]);
  200. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  201. }
  202. }
  203. else if (self._pageControlStyle==PageControlStyleWithPageNumber)
  204. {
  205. CGContextSetLineWidth(myContext, self._strokeWidth);
  206. if (i==_currentPage)
  207. {
  208. int _currentPageDiameter = _diameter*1.6;
  209. x = (self.frame.size.width-total_width)/2 + i*(_diameter+gap) - (_currentPageDiameter-_diameter)/2;
  210. CGContextSetFillColorWithColor(myContext, [_coreSelectedColor CGColor]);
  211. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2,_currentPageDiameter,_currentPageDiameter));
  212. CGContextSetStrokeColorWithColor(myContext, [_strokeSelectedColor CGColor]);
  213. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2,_currentPageDiameter,_currentPageDiameter));
  214. NSString *pageNumber = [NSString stringWithFormat:@"%i", i+1];
  215. CGContextSetFillColorWithColor(myContext, [[UIColor whiteColor] CGColor]);
  216. [pageNumber drawInRect:CGRectMake(x,(self.frame.size.height-_currentPageDiameter)/2-1,_currentPageDiameter,_currentPageDiameter) withFont:[UIFont systemFontOfSize:_currentPageDiameter-2] lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentCenter];
  217. }
  218. else
  219. {
  220. CGContextSetStrokeColorWithColor(myContext, [_strokeNormalColor CGColor]);
  221. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  222. }
  223. }
  224. else if (self._pageControlStyle==PageControlStylePressed1 || self._pageControlStyle==PageControlStylePressed2)
  225. {
  226. if (self._pageControlStyle==PageControlStylePressed1)
  227. {
  228. CGContextSetFillColorWithColor(myContext, [[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor]);
  229. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2-1,_diameter,_diameter));
  230. }
  231. else if (self._pageControlStyle==PageControlStylePressed2)
  232. {
  233. CGContextSetFillColorWithColor(myContext, [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1] CGColor]);
  234. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2+1,_diameter,_diameter));
  235. }
  236. if (i==_currentPage)
  237. {
  238. CGContextSetFillColorWithColor(myContext, [_coreSelectedColor CGColor]);
  239. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  240. CGContextSetStrokeColorWithColor(myContext, [_strokeSelectedColor CGColor]);
  241. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  242. }
  243. else
  244. {
  245. CGContextSetFillColorWithColor(myContext, [_coreNormalColor CGColor]);
  246. CGContextFillEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  247. CGContextSetStrokeColorWithColor(myContext, [_strokeNormalColor CGColor]);
  248. CGContextStrokeEllipseInRect(myContext, CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter));
  249. }
  250. }
  251. else if (self.pageControlStyle==PageControlStyleThumb)
  252. {
  253. if (self.thumbImage && self.selectedThumbImage)
  254. {
  255. if (i==_currentPage)
  256. {
  257. [self.selectedThumbImage drawInRect:CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter)];
  258. }
  259. else
  260. {
  261. [self.thumbImage drawInRect:CGRectMake(x,(self.frame.size.height-_diameter)/2,_diameter,_diameter)];
  262. }
  263. }
  264. }
  265. }
  266. }
  267. - (void)dealloc
  268. {
  269. self.coreSelectedColor = nil;
  270. self.coreNormalColor = nil;
  271. self.strokeNormalColor = nil;
  272. self.strokeSelectedColor = nil;
  273. [super dealloc];
  274. }
  275. - (PageControlStyle)pageControlStyle
  276. {
  277. return self._pageControlStyle;
  278. }
  279. - (void)setPageControlStyle:(PageControlStyle)style
  280. {
  281. self._pageControlStyle = style;
  282. [self setNeedsDisplay];
  283. }
  284. - (void)setCurrentPage:(int)page
  285. {
  286. self._currentPage = page;
  287. [self setNeedsDisplay];
  288. }
  289. - (int)currentPage
  290. {
  291. return self._currentPage;
  292. }
  293. - (void)setNumberOfPages:(int)numOfPages
  294. {
  295. self._numberOfPages = numOfPages;
  296. [self setNeedsDisplay];
  297. }
  298. - (int)numberOfPages
  299. {
  300. return self._numberOfPages;
  301. }
  302. @end