MyProgressView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // MyProgressView.m
  3. // TEST
  4. //
  5. // Created by Megatron on 12/1/14.
  6. // Copyright (c) 2014 Megatron. All rights reserved.
  7. //
  8. #import "MyProgressView.h"
  9. #import "Header.h"
  10. @implementation MyProgressView
  11. @synthesize dataArray;
  12. - (id)initWithFrame:(CGRect)frame
  13. {
  14. self = [super initWithFrame:frame];
  15. if (self) {
  16. self.array_Lab = [[NSMutableArray alloc] initWithCapacity:0];
  17. }
  18. return self;
  19. }
  20. -(void)drawRect:(CGRect)rect
  21. {
  22. if (self.dataArray == nil || self.dataArray.count == 0) {
  23. return;
  24. }
  25. // 移除原有的label
  26. for (UILabel *lab in self.array_Lab) {
  27. [lab removeFromSuperview];
  28. }
  29. // 坐标直线
  30. CGContextRef context = UIGraphicsGetCurrentContext();
  31. CGContextSetRGBStrokeColor(context, 185/255.0, 186/255.0, 187/255.0, 0.9);
  32. CGContextSetLineWidth(context, 2.5);
  33. CGContextMoveToPoint(context, 0, 80);
  34. CGContextAddLineToPoint(context, rect.size.width, 80);
  35. CGContextStrokePath(context);
  36. if (self.type == UNIT_TYPE_HOUR) {
  37. // 绘制下标和单位
  38. TimeInfo *info1 = [self.dataArray objectAtIndex:self.leftN];
  39. TimeInfo *info2 = [self.dataArray objectAtIndex:self.rightN];
  40. for (int i = info1.start_Time; i < info2.end_Time ; i = i + 3600) {
  41. if (i % 3600 == 0) {
  42. float pointX1 = (i - self.middleT) / (24 * 60 * 60.0) * 24 * Unit_Hour + self.frame.size.width * 0.5;
  43. CGContextSetLineWidth(context, 2);
  44. CGContextMoveToPoint(context, pointX1, 70);
  45. CGContextAddLineToPoint(context, pointX1, 80);
  46. CGContextStrokePath(context);
  47. UILabel *lab = [[UILabel alloc] init];
  48. lab.bounds = CGRectMake(0, 0, 30, 20);
  49. lab.center = CGPointMake(pointX1, 95);
  50. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  51. int h = (i / 3600);
  52. lab.text = [NSString stringWithFormat:@"%@:00",h >= 10?[NSString stringWithFormat:@"%i",h]:[NSString stringWithFormat:@"0%i",h]];
  53. lab.textAlignment = NSTextAlignmentCenter;
  54. lab.textColor = NormalFontColor;
  55. [self addSubview:lab];
  56. [self.array_Lab addObject:lab];
  57. }
  58. else
  59. {
  60. int s = i;
  61. do {
  62. s = s + 1;
  63. } while (s % 3600 != 0);
  64. float pointX2 = (s - self.middleT) / (24 * 60 * 60.0) * 24 * Unit_Hour + self.frame.size.width * 0.5;
  65. CGContextSetLineWidth(context, 2);
  66. CGContextMoveToPoint(context, pointX2, 70);
  67. CGContextAddLineToPoint(context, pointX2, 80);
  68. CGContextStrokePath(context);
  69. UILabel *lab = [[UILabel alloc] init];
  70. lab.bounds = CGRectMake(0, 0, 30, 20);
  71. lab.center = CGPointMake(pointX2, 95);
  72. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  73. int h = (s / 3600);
  74. lab.text = [NSString stringWithFormat:@"%@:00",h >= 10?[NSString stringWithFormat:@"%i",h]:[NSString stringWithFormat:@"0%i",h]];
  75. lab.textAlignment = NSTextAlignmentCenter;
  76. lab.textColor = NormalFontColor;
  77. [self addSubview:lab];
  78. [self.array_Lab addObject:lab];
  79. }
  80. }
  81. for (int i = (self.leftN - 1)>=0?self.leftN - 1:0;i <= self.rightN; i ++) {
  82. TimeInfo *info = [self.dataArray objectAtIndex:i];
  83. if (info.type == TYPE_ALARM) {
  84. CGContextSetRGBStrokeColor(context, 245/255.0, 38/255.0, 65/255.0, 1);
  85. }
  86. else if ( info.type == TYPE_NONE )
  87. {
  88. CGContextSetRGBStrokeColor(context, 177/255.0, 174/255.0, 177/255.0, 1);
  89. }
  90. else if ( info.type == TYPE_NORMAL )
  91. {
  92. CGContextSetRGBStrokeColor(context, 22/255.0, 159/255.0, 244/255.0, 1);
  93. }
  94. else if ( info.type == TYPE_DETECTION)
  95. {
  96. CGContextSetRGBStrokeColor(context, 232/255.0, 114/255.0, 0/255.0, 1);
  97. }
  98. else if ( info.type == TYPE_HAND)
  99. {
  100. CGContextSetRGBStrokeColor(context, 9/255.0, 250/255.0, 149/255.0, 1);
  101. }
  102. float startX = ((info.start_Time - self.middleT) / (24.0*60*60) * Unit_Hour * 24) + rect.size.width * 0.5;
  103. float endX = ((info.end_Time - self.middleT) / (24.0*60*60) * Unit_Hour * 24) + rect.size.width * 0.5;
  104. CGContextSetLineWidth(context, 25);
  105. CGContextMoveToPoint(context, startX, 50);
  106. CGContextAddLineToPoint(context, endX + 1, 50);
  107. CGContextStrokePath(context);
  108. }
  109. }
  110. else if (self.type == UNIT_TYPE_MINUTE)
  111. {
  112. // 绘制下标和单位
  113. TimeInfo *info1 = [self.dataArray objectAtIndex:self.leftN];
  114. TimeInfo *info2 = [self.dataArray objectAtIndex:self.rightN];
  115. for (int i = info1.start_Time; i < info2.end_Time ; i = i + 600) {
  116. if (i % 600 == 0) {
  117. float pointX1 = (i - self.middleT) / (24 * 60 * 60.0) * 24 * 6 * Unit_Minute + self.frame.size.width * 0.5;
  118. CGContextSetLineWidth(context, 2);
  119. CGContextMoveToPoint(context, pointX1, 70);
  120. CGContextAddLineToPoint(context, pointX1, 80);
  121. CGContextStrokePath(context);
  122. UILabel *lab = [[UILabel alloc] init];
  123. lab.bounds = CGRectMake(0, 0, 30, 20);
  124. lab.center = CGPointMake(pointX1, 95);
  125. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  126. lab.text = [self getMinuteStringWithSecond:(i - (i % 60))];
  127. lab.textAlignment = NSTextAlignmentCenter;
  128. lab.textColor = NormalFontColor;
  129. [self addSubview:lab];
  130. [self.array_Lab addObject:lab];
  131. }
  132. else
  133. {
  134. int s = i;
  135. do {
  136. s = s + 1;
  137. } while (s % 600 != 0);
  138. float pointX2 = (s - self.middleT) / (24 * 60 * 60.0) * 24 * 6 * Unit_Minute + self.frame.size.width * 0.5;
  139. CGContextSetLineWidth(context, 2);
  140. CGContextMoveToPoint(context, pointX2, 70);
  141. CGContextAddLineToPoint(context, pointX2, 80);
  142. CGContextStrokePath(context);
  143. UILabel *lab = [[UILabel alloc] init];
  144. lab.bounds = CGRectMake(0, 0, 30, 20);
  145. lab.center = CGPointMake(pointX2, 95);
  146. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  147. s = s - (s % 60);
  148. lab.text = [self getMinuteStringWithSecond:s];
  149. lab.textAlignment = NSTextAlignmentCenter;
  150. lab.textColor = NormalFontColor;
  151. [self addSubview:lab];
  152. [self.array_Lab addObject:lab];
  153. }
  154. }
  155. for (int i = (self.leftN - 1)>=0?self.leftN - 1:0;i <= self.rightN; i ++) {
  156. TimeInfo *info = [self.dataArray objectAtIndex:i];
  157. if (info.type == TYPE_ALARM) {
  158. CGContextSetRGBStrokeColor(context, 245/255.0, 38/255.0, 65/255.0, 1);
  159. }
  160. else if ( info.type == TYPE_NONE )
  161. {
  162. CGContextSetRGBStrokeColor(context, 177/255.0, 174/255.0, 177/255.0, 1);
  163. }
  164. else if ( info.type == TYPE_NORMAL )
  165. {
  166. CGContextSetRGBStrokeColor(context, 22/255.0, 159/255.0, 244/255.0, 1);
  167. }
  168. else if ( info.type == TYPE_DETECTION)
  169. {
  170. CGContextSetRGBStrokeColor(context, 232/255.0, 114/255.0, 0/255.0, 1);
  171. }
  172. else if ( info.type == TYPE_HAND)
  173. {
  174. CGContextSetRGBStrokeColor(context, 9/255.0, 250/255.0, 149/255.0, 1);
  175. }
  176. float startX = ((info.start_Time - self.middleT) / (24.0*60*60) * Unit_Minute * 6 * 24) + rect.size.width * 0.5;
  177. float endX = ((info.end_Time - self.middleT) / (24.0*60*60) * Unit_Minute * 6 * 24) + rect.size.width * 0.5;
  178. CGContextSetLineWidth(context, 25); // 25 时间轴的粗细程度
  179. CGContextMoveToPoint(context, startX, 50);
  180. CGContextAddLineToPoint(context, endX + 1, 50); // +1 防止出现白色细缝
  181. CGContextStrokePath(context);
  182. }
  183. }
  184. else if (self.type == UNIT_TYPE_SECOND) {
  185. // 绘制下标和单位
  186. TimeInfo *info1 = [self.dataArray objectAtIndex:self.leftN];
  187. TimeInfo *info2 = [self.dataArray objectAtIndex:self.rightN];
  188. for (int i = info1.start_Time; i < info2.end_Time ; i = i + 10) {
  189. if (i % 10 == 0) {
  190. float pointX1 = (i - self.middleT) / (24 * 60 * 60.0) * 24 * 60 * 6 * Unit_Second + self.frame.size.width * 0.5;
  191. if (pointX1 >= 0 || pointX1 <= rect.size.width) {
  192. CGContextSetLineWidth(context, 2);
  193. CGContextMoveToPoint(context, pointX1, 70);
  194. CGContextAddLineToPoint(context, pointX1, 80);
  195. CGContextStrokePath(context);
  196. UILabel *lab = [[UILabel alloc] init];
  197. lab.bounds = CGRectMake(0, 0, 50, 20);
  198. lab.center = CGPointMake(pointX1, 95);
  199. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  200. lab.text = [self getSecondStringWithSecond:(i - (i % 10))];
  201. lab.textAlignment = NSTextAlignmentCenter;
  202. lab.textColor = NormalFontColor;
  203. [self addSubview:lab];
  204. [self.array_Lab addObject:lab];
  205. }
  206. }
  207. else
  208. {
  209. int s = i;
  210. do {
  211. s = s + 1;
  212. } while (s % 600 != 0);
  213. float pointX2 = (s - self.middleT) / (24 * 60 * 60.0) * 24 * 60 * 6 * Unit_Second + self.frame.size.width * 0.5;
  214. if (pointX2 >= 0 || pointX2 <= rect.size.width) {
  215. CGContextSetLineWidth(context, 2);
  216. CGContextMoveToPoint(context, pointX2, 70);
  217. CGContextAddLineToPoint(context, pointX2, 80);
  218. CGContextStrokePath(context);
  219. UILabel *lab = [[UILabel alloc] init];
  220. lab.bounds = CGRectMake(0, 0, 50, 20);
  221. lab.center = CGPointMake(pointX2, 95);
  222. lab.font = [UIFont fontWithName:@"Marion" size:12.0];
  223. s = s - (s % 10);
  224. lab.text = [self getSecondStringWithSecond:s];
  225. lab.textAlignment = NSTextAlignmentCenter;
  226. lab.textColor = NormalFontColor;
  227. [self addSubview:lab];
  228. [self.array_Lab addObject:lab];
  229. }
  230. }
  231. }
  232. for (int i = (self.leftN - 1) >= 0 ? self.leftN - 1 : 0;i <= self.rightN; i ++) {
  233. TimeInfo *info = [self.dataArray objectAtIndex:i];
  234. if (info.type == TYPE_ALARM) {
  235. CGContextSetRGBStrokeColor(context, 245/255.0, 38/255.0, 65/255.0, 1);
  236. }
  237. else if ( info.type == TYPE_NONE )
  238. {
  239. CGContextSetRGBStrokeColor(context, 177/255.0, 174/255.0, 177/255.0, 1);
  240. }
  241. else if ( info.type == TYPE_NORMAL )
  242. {
  243. CGContextSetRGBStrokeColor(context, 22/255.0, 159/255.0, 244/255.0, 1);
  244. }
  245. else if ( info.type == TYPE_DETECTION)
  246. {
  247. CGContextSetRGBStrokeColor(context, 232/255.0, 114/255.0, 0/255.0, 1);
  248. }
  249. else if ( info.type == TYPE_HAND)
  250. {
  251. CGContextSetRGBStrokeColor(context, 9/255.0, 250/255.0, 149/255.0, 1);
  252. }
  253. float startX = ((info.start_Time - self.middleT) / (24.0*60*60) * Unit_Second * 6 * 60 * 24) + rect.size.width * 0.5;
  254. float endX = ((info.end_Time - self.middleT) / (24.0*60*60) * Unit_Second * 6 * 60 * 24) + rect.size.width * 0.5;
  255. if (startX < 0) {
  256. startX = 0;
  257. }
  258. if (endX > rect.size.width) {
  259. endX = rect.size.width;
  260. }
  261. CGContextSetLineWidth(context, 25);
  262. CGContextMoveToPoint(context, startX, 50);
  263. CGContextAddLineToPoint(context, endX + 1, 50);
  264. CGContextStrokePath(context);
  265. }
  266. }
  267. }
  268. -(void)refreshViewWithDataFrom:(int)leftNum to:(int)rightNum andMiddleNum:(int)standardNum andMiddleTime:(int)second andType:(enum Unit_Type)type
  269. {
  270. self.standardN = standardNum;
  271. self.leftN = leftNum;
  272. self.rightN = rightNum;
  273. self.type = type;
  274. self.middleT = second;
  275. if (self.middleT < 0) {
  276. self.middleT = 0;
  277. }
  278. else if (self.middleT > (24*60*60))
  279. {
  280. self.middleT = 24*60*60;
  281. }
  282. [self setNeedsDisplay];
  283. }
  284. -(NSString *)getMinuteStringWithSecond:(int)second
  285. {
  286. int h = second / 3600;
  287. int m = (second % 3600) / 60;
  288. NSString *str = [NSString stringWithFormat:@"%@:%@",h>=10?[NSString stringWithFormat:@"%i",h]:[NSString stringWithFormat:@"0%i",h],m>=10?[NSString stringWithFormat:@"%i",m]:[NSString stringWithFormat:@"0%i",m]];
  289. return str;
  290. }
  291. -(NSString *)getSecondStringWithSecond:(int)second
  292. {
  293. int h = second / 3600;
  294. int m = (second % 3600) / 60;
  295. int s = ((second % 3600) % 60);
  296. NSString *str = [NSString stringWithFormat:@"%@:%@:%@",h>=10?[NSString stringWithFormat:@"%i",h]:[NSString stringWithFormat:@"0%i",h],m>=10?[NSString stringWithFormat:@"%i",m]:[NSString stringWithFormat:@"0%i",m],s>=10?[NSString stringWithFormat:@"%i",s]:[NSString stringWithFormat:@"0%i",s]];
  297. return str;
  298. }
  299. -(void)modefy
  300. {
  301. }
  302. @end