QBPlasticPopupMenu.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. //
  2. // QBPlasticPopupMenu.m
  3. // QBPopupMenu
  4. //
  5. // Created by Tanaka Katsuma on 2013/11/25.
  6. // Copyright (c) 2013年 Katsuma Tanaka. All rights reserved.
  7. //
  8. #import "QBPlasticPopupMenu.h"
  9. @implementation QBPlasticPopupMenu
  10. #pragma mark - Creating Paths
  11. - (CGMutablePathRef)upperHeadPathInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
  12. {
  13. CGMutablePathRef path = CGPathCreateMutable();
  14. CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y + cornerRadius);
  15. CGPathAddArcToPoint(path, NULL, rect.origin.x, rect.origin.y, rect.origin.x + cornerRadius, rect.origin.y, cornerRadius);
  16. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
  17. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
  18. CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
  19. CGPathCloseSubpath(path);
  20. return path;
  21. }
  22. - (CGMutablePathRef)lowerHeadPathInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
  23. {
  24. CGMutablePathRef path = CGPathCreateMutable();
  25. CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
  26. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
  27. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
  28. CGPathAddLineToPoint(path, NULL, rect.origin.x + cornerRadius, rect.origin.y + rect.size.height);
  29. CGPathAddArcToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height, rect.origin.x, rect.origin.y + rect.size.height - cornerRadius, cornerRadius);
  30. CGPathCloseSubpath(path);
  31. return path;
  32. }
  33. - (CGMutablePathRef)upperTailPathInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
  34. {
  35. CGMutablePathRef path = CGPathCreateMutable();
  36. CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
  37. CGPathMoveToPoint(path, NULL, rect.origin.x + rect.size.width - cornerRadius, rect.origin.y);
  38. CGPathAddArcToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y, rect.origin.x + rect.size.width, rect.origin.y + cornerRadius, cornerRadius);
  39. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
  40. CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
  41. CGPathCloseSubpath(path);
  42. return path;
  43. }
  44. - (CGMutablePathRef)lowerTailPathInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius
  45. {
  46. CGMutablePathRef path = CGPathCreateMutable();
  47. CGPathMoveToPoint(path, NULL, rect.origin.x, rect.origin.y);
  48. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y);
  49. CGPathAddLineToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height - cornerRadius);
  50. CGPathAddArcToPoint(path, NULL, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, rect.origin.x + rect.size.width - cornerRadius, rect.origin.y + rect.size.height, cornerRadius);
  51. CGPathAddLineToPoint(path, NULL, rect.origin.x, rect.origin.y + rect.size.height);
  52. CGPathCloseSubpath(path);
  53. return path;
  54. }
  55. #pragma mark - Drawing Popup Menu Image
  56. - (void)drawArrowInRect:(CGRect)rect direction:(QBPopupMenuArrowDirection)direction highlighted:(BOOL)highlighted
  57. {
  58. CGContextRef context = UIGraphicsGetCurrentContext();
  59. // Border
  60. CGContextSaveGState(context); {
  61. CGRect arrowRect = CGRectZero;
  62. switch (direction) {
  63. case QBPopupMenuArrowDirectionDown:
  64. arrowRect = CGRectMake(rect.origin.x, rect.origin.y - 0.6, rect.size.width, rect.size.height);
  65. break;
  66. case QBPopupMenuArrowDirectionUp:
  67. arrowRect = CGRectMake(rect.origin.x, rect.origin.y + 0.6, rect.size.width, rect.size.height);
  68. break;
  69. case QBPopupMenuArrowDirectionLeft:
  70. arrowRect = CGRectMake(rect.origin.x + 0.6, rect.origin.y - 0.5, rect.size.width, rect.size.height);
  71. break;
  72. case QBPopupMenuArrowDirectionRight:
  73. arrowRect = CGRectMake(rect.origin.x - 0.6, rect.origin.y - 0.5, rect.size.width, rect.size.height);
  74. break;
  75. default:
  76. break;
  77. }
  78. CGMutablePathRef path = [self arrowPathInRect:arrowRect direction:direction];
  79. CGContextAddPath(context, path);
  80. CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
  81. CGContextFillPath(context);
  82. CGPathRelease(path);
  83. } CGContextRestoreGState(context);
  84. // Highlight
  85. CGContextSaveGState(context); {
  86. CGRect arrowRect = CGRectZero;
  87. switch (direction) {
  88. case QBPopupMenuArrowDirectionUp:
  89. arrowRect = CGRectMake(rect.origin.x, rect.origin.y + 2, rect.size.width, rect.size.height);
  90. break;
  91. case QBPopupMenuArrowDirectionLeft:
  92. arrowRect = CGRectMake(rect.origin.x + 2, rect.origin.y - 0.5 + 1, rect.size.width - 1, rect.size.height - 2);
  93. break;
  94. case QBPopupMenuArrowDirectionRight:
  95. arrowRect = CGRectMake(rect.origin.x - 1, rect.origin.y - 0.5 + 1, rect.size.width - 1, rect.size.height - 2);
  96. break;
  97. default:
  98. break;
  99. }
  100. CGMutablePathRef path = [self arrowPathInRect:arrowRect direction:direction];
  101. CGContextAddPath(context, path);
  102. if (highlighted) {
  103. CGContextSetRGBFillColor(context, 0.384, 0.608, 0.906, 1.0);
  104. } else {
  105. CGContextSetRGBFillColor(context, 0.471, 0.471, 0.471, 1.0);
  106. }
  107. CGContextFillPath(context);
  108. CGPathRelease(path);
  109. } CGContextRestoreGState(context);
  110. // Body
  111. CGContextSaveGState(context); {
  112. switch (direction) {
  113. case QBPopupMenuArrowDirectionDown:
  114. {
  115. if (highlighted) {
  116. CGMutablePathRef path = [self arrowPathInRect:CGRectMake(rect.origin.x, rect.origin.y - 2, rect.size.width, rect.size.height) direction:direction];
  117. CGContextAddPath(context, path);
  118. CGContextClip(context);
  119. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  120. CGFloat components[8];
  121. components[0] = 0.027; components[1] = 0.169; components[2] = 0.733; components[3] = 1;
  122. components[4] = 0.020; components[5] = 0.114; components[6] = 0.675; components[7] = 1;
  123. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  124. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y - 2);
  125. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  126. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  127. CGGradientRelease(gradient);
  128. CGColorSpaceRelease(colorSpace);
  129. CGPathRelease(path);
  130. }
  131. }
  132. break;
  133. case QBPopupMenuArrowDirectionUp:
  134. {
  135. CGMutablePathRef path = [self arrowPathInRect:CGRectMake(rect.origin.x + 1.4, rect.origin.y + 2 + 1.4, rect.size.width - 2.8, rect.size.height - 1.4) direction:direction];
  136. CGContextAddPath(context, path);
  137. CGContextClip(context);
  138. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  139. CGFloat components[8];
  140. if (highlighted) {
  141. components[0] = 0.290; components[1] = 0.580; components[2] = 1.000; components[3] = 1;
  142. components[4] = 0.216; components[5] = 0.471; components[6] = 0.871; components[7] = 1;
  143. } else {
  144. components[0] = 0.401; components[1] = 0.401; components[2] = 0.401; components[3] = 1;
  145. components[4] = 0.314; components[5] = 0.314; components[6] = 0.314; components[7] = 1;
  146. }
  147. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  148. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + 2);
  149. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  150. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  151. CGGradientRelease(gradient);
  152. CGColorSpaceRelease(colorSpace);
  153. CGPathRelease(path);
  154. }
  155. break;
  156. case QBPopupMenuArrowDirectionLeft:
  157. {
  158. CGMutablePathRef path = [self arrowPathInRect:CGRectMake(rect.origin.x + 2, rect.origin.y - 0.5 + 2, rect.size.width - 1, rect.size.height - 2) direction:direction];
  159. CGContextAddPath(context, path);
  160. CGContextClip(context);
  161. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  162. CGFloat components[16];
  163. if (highlighted) {
  164. components[0] = 0.082; components[1] = 0.376; components[2] = 0.859; components[3] = 1;
  165. components[4] = 0.004; components[5] = 0.333; components[6] = 0.851; components[7] = 1;
  166. components[8] = 0.000; components[9] = 0.282; components[10] = 0.839; components[11] = 1;
  167. components[12] = 0.000; components[13] = 0.216; components[14] = 0.796; components[15] = 1;
  168. } else {
  169. components[0] = 0.216; components[1] = 0.216; components[2] = 0.216; components[3] = 1;
  170. components[4] = 0.165; components[5] = 0.165; components[6] = 0.165; components[7] = 1;
  171. components[8] = 0.102; components[9] = 0.102; components[10] = 0.102; components[11] = 1;
  172. components[12] = 0.051; components[13] = 0.051; components[14] = 0.051; components[15] = 1;
  173. }
  174. CGFloat locations[4] = { 0.0, 0.5, 0.5, 1.0 };
  175. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 4);
  176. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y - 1);
  177. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  178. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  179. CGGradientRelease(gradient);
  180. CGColorSpaceRelease(colorSpace);
  181. CGPathRelease(path);
  182. }
  183. break;
  184. case QBPopupMenuArrowDirectionRight:
  185. {
  186. CGMutablePathRef path = [self arrowPathInRect:CGRectMake(rect.origin.x - 1, rect.origin.y - 0.5 + 2, rect.size.width - 1, rect.size.height - 2) direction:direction];
  187. CGContextAddPath(context, path);
  188. CGContextClip(context);
  189. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  190. CGFloat components[16];
  191. if (highlighted) {
  192. components[0] = 0.082; components[1] = 0.376; components[2] = 0.859; components[3] = 1;
  193. components[4] = 0.004; components[5] = 0.333; components[6] = 0.851; components[7] = 1;
  194. components[8] = 0.000; components[9] = 0.282; components[10] = 0.839; components[11] = 1;
  195. components[12] = 0.000; components[13] = 0.216; components[14] = 0.796; components[15] = 1;
  196. } else {
  197. components[0] = 0.216; components[1] = 0.216; components[2] = 0.216; components[3] = 1;
  198. components[4] = 0.165; components[5] = 0.165; components[6] = 0.165; components[7] = 1;
  199. components[8] = 0.102; components[9] = 0.102; components[10] = 0.102; components[11] = 1;
  200. components[12] = 0.051; components[13] = 0.051; components[14] = 0.051; components[15] = 1;
  201. }
  202. CGFloat locations[4] = { 0.0, 0.5, 0.5, 1.0 };
  203. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 4);
  204. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y - 1);
  205. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  206. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  207. CGGradientRelease(gradient);
  208. CGColorSpaceRelease(colorSpace);
  209. CGPathRelease(path);
  210. }
  211. break;
  212. default:
  213. break;
  214. }
  215. } CGContextRestoreGState(context);
  216. }
  217. - (void)drawHeadInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius highlighted:(BOOL)highlighted
  218. {
  219. CGContextRef context = UIGraphicsGetCurrentContext();
  220. // Border
  221. // CGContextSaveGState(context); {
  222. // CGMutablePathRef path = [self headPathInRect:rect cornerRadius:cornerRadius];
  223. // CGContextAddPath(context, path);
  224. //
  225. // CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
  226. // CGContextFillPath(context);
  227. //
  228. // CGPathRelease(path);
  229. // } CGContextRestoreGState(context);
  230. // Highlight
  231. CGContextSaveGState(context); {
  232. CGMutablePathRef path = [self headPathInRect:CGRectMake(rect.origin.x + 1, rect.origin.y + 1, rect.size.width - 1, rect.size.height - 2)
  233. cornerRadius:cornerRadius - 1];
  234. CGContextAddPath(context, path);
  235. // if (highlighted) {
  236. // CGContextSetRGBFillColor(context, 0.384, 0.608, 0.906, 1.0);
  237. // } else {
  238. // CGContextSetRGBFillColor(context, 0.471, 0.471, 0.471, 1.0);
  239. // }
  240. // CGContextFillPath(context);
  241. CGPathRelease(path);
  242. } CGContextRestoreGState(context);
  243. // Upper head
  244. CGContextSaveGState(context); {
  245. CGMutablePathRef path = [self upperHeadPathInRect:CGRectMake(rect.origin.x + 1, rect.origin.y + 2, rect.size.width - 1, rect.size.height / 2 - 2) cornerRadius:cornerRadius - 1];
  246. CGContextAddPath(context, path);
  247. CGContextClip(context);
  248. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  249. CGFloat components[8];
  250. // if (highlighted) {
  251. // components[0] = 0.216; components[1] = 0.471; components[2] = 0.871; components[3] = 1;
  252. // components[4] = 0.059; components[5] = 0.353; components[6] = 0.839; components[7] = 1;
  253. // } else {
  254. // components[0] = 0.314; components[1] = 0.314; components[2] = 0.314; components[3] = 1;
  255. // components[4] = 0.165; components[5] = 0.165; components[6] = 0.165; components[7] = 1;
  256. // }
  257. if (highlighted) {
  258. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  259. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  260. } else {
  261. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  262. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  263. }
  264. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  265. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + 2);
  266. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2 - 2);
  267. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  268. CGGradientRelease(gradient);
  269. CGColorSpaceRelease(colorSpace);
  270. CGPathRelease(path);
  271. } CGContextRestoreGState(context);
  272. // Lower head
  273. CGContextSaveGState(context); {
  274. CGMutablePathRef path = [self lowerHeadPathInRect:CGRectMake(rect.origin.x + 1, rect.origin.y + rect.size.height / 2, rect.size.width - 1, rect.size.height / 2 - 1) cornerRadius:cornerRadius - 1];
  275. CGContextAddPath(context, path);
  276. CGContextClip(context);
  277. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  278. CGFloat components[8];
  279. // if (highlighted) {
  280. // components[0] = 0.047; components[1] = 0.306; components[2] = 0.827; components[3] = 1;
  281. // components[4] = 0.027; components[5] = 0.176; components[6] = 0.737; components[7] = 1;
  282. // } else {
  283. // components[0] = 0.102; components[1] = 0.102; components[2] = 0.102; components[3] = 1;
  284. // components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  285. // }
  286. if (highlighted) {
  287. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  288. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  289. } else {
  290. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  291. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  292. }
  293. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  294. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2);
  295. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height - 1);
  296. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  297. CGGradientRelease(gradient);
  298. CGColorSpaceRelease(colorSpace);
  299. CGPathRelease(path);
  300. } CGContextRestoreGState(context);
  301. }
  302. - (void)drawTailInRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius highlighted:(BOOL)highlighted
  303. {
  304. CGContextRef context = UIGraphicsGetCurrentContext();
  305. // Border
  306. // CGContextSaveGState(context); {
  307. // CGMutablePathRef path = [self tailPathInRect:rect cornerRadius:cornerRadius];
  308. // CGContextAddPath(context, path);
  309. //
  310. // CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
  311. // CGContextFillPath(context);
  312. //
  313. // CGPathRelease(path);
  314. // } CGContextRestoreGState(context);
  315. // Highlight
  316. CGContextSaveGState(context); {
  317. CGMutablePathRef path = [self tailPathInRect:CGRectMake(rect.origin.x, rect.origin.y + 1, rect.size.width - 1, rect.size.height - 2) cornerRadius:cornerRadius - 1];
  318. CGContextAddPath(context, path);
  319. // if (highlighted) {
  320. // CGContextSetRGBFillColor(context, 0.384, 0.608, 0.906, 1.0);
  321. // } else {
  322. // CGContextSetRGBFillColor(context, 0.471, 0.471, 0.471, 1.0);
  323. // }
  324. // CGContextFillPath(context);
  325. CGPathRelease(path);
  326. } CGContextRestoreGState(context);
  327. // Upper body
  328. CGContextSaveGState(context); {
  329. CGMutablePathRef path = [self upperTailPathInRect:CGRectMake(rect.origin.x, rect.origin.y + 2, rect.size.width - 1, rect.size.height / 2 - 2) cornerRadius:cornerRadius - 1];
  330. CGContextAddPath(context, path);
  331. CGContextClip(context);
  332. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  333. CGFloat components[8];
  334. // if (highlighted) {
  335. // components[0] = 0.216; components[1] = 0.471; components[2] = 0.871; components[3] = 1;
  336. // components[4] = 0.059; components[5] = 0.353; components[6] = 0.839; components[7] = 1;
  337. // } else {
  338. // components[0] = 0.314; components[1] = 0.314; components[2] = 0.314; components[3] = 1;
  339. // components[4] = 0.165; components[5] = 0.165; components[6] = 0.165; components[7] = 1;
  340. // }
  341. if (highlighted) {
  342. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  343. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  344. } else {
  345. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  346. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  347. }
  348. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  349. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + 2);
  350. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2 - 2);
  351. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  352. CGGradientRelease(gradient);
  353. CGColorSpaceRelease(colorSpace);
  354. CGPathRelease(path);
  355. } CGContextRestoreGState(context);
  356. // Lower body
  357. CGContextSaveGState(context); {
  358. CGMutablePathRef path = [self lowerTailPathInRect:CGRectMake(rect.origin.x, rect.origin.y + rect.size.height / 2, rect.size.width - 1, rect.size.height / 2 - 1) cornerRadius:cornerRadius - 1];
  359. CGContextAddPath(context, path);
  360. CGContextClip(context);
  361. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  362. CGFloat components[8];
  363. // if (highlighted) {
  364. // components[0] = 0.047; components[1] = 0.306; components[2] = 0.827; components[3] = 1;
  365. // components[4] = 0.027; components[5] = 0.176; components[6] = 0.737; components[7] = 1;
  366. // } else {
  367. // components[0] = 0.102; components[1] = 0.102; components[2] = 0.102; components[3] = 1;
  368. // components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  369. // }
  370. if (highlighted) {
  371. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  372. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  373. } else {
  374. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  375. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  376. }
  377. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  378. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2);
  379. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height - 1);
  380. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  381. CGGradientRelease(gradient);
  382. CGColorSpaceRelease(colorSpace);
  383. CGPathRelease(path);
  384. } CGContextRestoreGState(context);
  385. }
  386. - (void)drawBodyInRect:(CGRect)rect firstItem:(BOOL)firstItem lastItem:(BOOL)lastItem highlighted:(BOOL)highlighted
  387. {
  388. CGContextRef context = UIGraphicsGetCurrentContext();
  389. // Border
  390. // CGContextSaveGState(context); {
  391. // CGMutablePathRef path = [self bodyPathInRect:rect];
  392. // CGContextAddPath(context, path);
  393. //
  394. // CGContextSetRGBFillColor(context, 0, 0, 0, 1.0);
  395. // CGContextFillPath(context);
  396. //
  397. // CGPathRelease(path);
  398. // } CGContextRestoreGState(context);
  399. // Highlight
  400. CGContextSaveGState(context); {
  401. CGMutablePathRef path = [self bodyPathInRect:CGRectMake(rect.origin.x, rect.origin.y + 1, rect.size.width, rect.size.height - 2)];
  402. CGContextAddPath(context, path);
  403. // if (highlighted) {
  404. // CGContextSetRGBFillColor(context, 0.384, 0.608, 0.906, 1.0);
  405. // } else {
  406. // CGContextSetRGBFillColor(context, 0.471, 0.471, 0.471, 1.0);
  407. // }
  408. // CGContextFillPath(context);
  409. CGPathRelease(path);
  410. } CGContextRestoreGState(context);
  411. // Upper body
  412. CGContextSaveGState(context); {
  413. CGMutablePathRef path = [self bodyPathInRect:CGRectMake(rect.origin.x, rect.origin.y + 2, rect.size.width, rect.size.height / 2 - 2)];
  414. CGContextAddPath(context, path);
  415. CGContextClip(context);
  416. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  417. CGFloat components[8];
  418. // if (highlighted) {
  419. // components[0] = 0.216; components[1] = 0.471; components[2] = 0.871; components[3] = 1;
  420. // components[4] = 0.059; components[5] = 0.353; components[6] = 0.839; components[7] = 1;
  421. // } else {
  422. // components[0] = 0.314; components[1] = 0.314; components[2] = 0.314; components[3] = 1;
  423. // components[4] = 0.165; components[5] = 0.165; components[6] = 0.165; components[7] = 1;
  424. // }
  425. if (highlighted) {
  426. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  427. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  428. } else {
  429. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  430. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  431. }
  432. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  433. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + 2);
  434. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2 - 2);
  435. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  436. CGGradientRelease(gradient);
  437. CGColorSpaceRelease(colorSpace);
  438. CGPathRelease(path);
  439. } CGContextRestoreGState(context);
  440. // Lower body
  441. CGContextSaveGState(context); {
  442. CGMutablePathRef path = [self bodyPathInRect:CGRectMake(rect.origin.x, rect.origin.y + rect.size.height / 2, rect.size.width, rect.size.height / 2 - 1)];
  443. CGContextAddPath(context, path);
  444. CGContextClip(context);
  445. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  446. CGFloat components[8];
  447. // if (highlighted) {
  448. // components[0] = 0.047; components[1] = 0.306; components[2] = 0.827; components[3] = 1;
  449. // components[4] = 0.027; components[5] = 0.176; components[6] = 0.737; components[7] = 1;
  450. // } else {
  451. // components[0] = 0.102; components[1] = 0.102; components[2] = 0.102; components[3] = 1;
  452. // components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  453. // }
  454. if (highlighted) {
  455. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  456. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  457. } else {
  458. components[0] = 0; components[1] = 0; components[2] = 0; components[3] = 1;
  459. components[4] = 0; components[5] = 0; components[6] = 0; components[7] = 1;
  460. }
  461. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2);
  462. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height / 2);
  463. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height - 1);
  464. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  465. CGGradientRelease(gradient);
  466. CGColorSpaceRelease(colorSpace);
  467. CGPathRelease(path);
  468. } CGContextRestoreGState(context);
  469. // Draw separator
  470. if (!firstItem) {
  471. [self drawLeftSeparatorInRect:CGRectMake(rect.origin.x, rect.origin.y + 2, 0.5, rect.size.height - 3) highlighted:highlighted];
  472. }
  473. // if (!lastItem) {
  474. // [self drawRightSeparatorInRect:CGRectMake(rect.origin.x + rect.size.width - 1, rect.origin.y + 2, 1, rect.size.height - 3) highlighted:highlighted];
  475. // }
  476. }
  477. - (void)drawLeftSeparatorInRect:(CGRect)rect highlighted:(BOOL)highlighted
  478. {
  479. CGContextRef context = UIGraphicsGetCurrentContext();
  480. // Left separator
  481. CGContextSaveGState(context); {
  482. CGContextAddRect(context, rect);
  483. CGContextClip(context);
  484. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  485. // CGFloat components[16];
  486. // if (highlighted) {
  487. // components[0] = 0.22; components[1] = 0.47; components[2] = 0.87; components[3] = 1;
  488. // components[4] = 0.12; components[5] = 0.50; components[6] = 0.89; components[7] = 1;
  489. // components[8] = 0.09; components[9] = 0.47; components[10] = 0.88; components[11] = 1;
  490. // components[12] = 0.03; components[13] = 0.18; components[14] = 0.74; components[15] = 1;
  491. // } else {
  492. // components[0] = 0.31; components[1] = 0.31; components[2] = 0.31; components[3] = 1;
  493. // components[4] = 0.31; components[5] = 0.31; components[6] = 0.31; components[7] = 1;
  494. // components[8] = 0.24; components[9] = 0.24; components[10] = 0.24; components[11] = 1;
  495. // components[12] = 0.05; components[13] = 0.05; components[14] = 0.05; components[15] = 1;
  496. // }
  497. CGFloat components[16];
  498. if (highlighted) {
  499. components[0] = 1; components[1] = 1; components[2] = 1; components[3] = 1;
  500. components[4] = 1; components[5] = 1; components[6] = 1; components[7] = 1;
  501. components[8] = 1; components[9] = 1; components[10] = 1; components[11] = 1;
  502. components[12] = 1; components[13] = 1; components[14] = 1; components[15] = 1;
  503. } else {
  504. components[0] = 1; components[1] = 1; components[2] = 1; components[3] = 1;
  505. components[4] = 1; components[5] = 1; components[6] = 1; components[7] = 1;
  506. components[8] = 1; components[9] = 1; components[10] = 1; components[11] = 1;
  507. components[12] = 1; components[13] = 1; components[14] = 1; components[15] = 1;
  508. }
  509. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 4);
  510. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y);
  511. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  512. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  513. CGGradientRelease(gradient);
  514. CGColorSpaceRelease(colorSpace);
  515. } CGContextRestoreGState(context);
  516. }
  517. - (void)drawRightSeparatorInRect:(CGRect)rect highlighted:(BOOL)highlighted
  518. {
  519. CGContextRef context = UIGraphicsGetCurrentContext();
  520. // Right separator
  521. CGContextSaveGState(context); {
  522. CGContextAddRect(context, rect);
  523. CGContextClip(context);
  524. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  525. CGFloat components[16];
  526. if (highlighted) {
  527. components[0] = 0.22; components[1] = 0.47; components[2] = 0.87; components[3] = 1;
  528. components[4] = 0.03; components[5] = 0.18; components[6] = 0.72; components[7] = 1;
  529. components[8] = 0.02; components[9] = 0.15; components[10] = 0.73; components[11] = 1;
  530. components[12] = 0.03; components[13] = 0.17; components[14] = 0.72; components[15] = 1;
  531. } else {
  532. components[0] = 0.31; components[1] = 0.31; components[2] = 0.31; components[3] = 1;
  533. components[4] = 0.06; components[5] = 0.06; components[6] = 0.06; components[7] = 1;
  534. components[8] = 0.04; components[9] = 0.04; components[10] = 0.04; components[11] = 1;
  535. components[12] = 0; components[13] = 0; components[14] = 0; components[15] = 1;
  536. }
  537. CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 4);
  538. CGPoint startPoint = CGPointMake(rect.origin.x, rect.origin.y);
  539. CGPoint endPoint = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
  540. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);
  541. CGGradientRelease(gradient);
  542. CGColorSpaceRelease(colorSpace);
  543. } CGContextRestoreGState(context);
  544. }
  545. @end