AFView.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // AFView.m
  3. // animationFrame
  4. //
  5. // Created by pingshw on 14-5-27.
  6. // Copyright (c) 2014年 pingshw. All rights reserved.
  7. //
  8. #import "AFView.h"
  9. #import "AFDescribeObj.h"
  10. @interface CCView : UIView
  11. @end
  12. @implementation AFView
  13. {
  14. CGFloat w;
  15. CGFloat h;
  16. }
  17. @synthesize min_Y;
  18. @synthesize max_Y;
  19. @synthesize min_X;
  20. @synthesize max_X;
  21. @synthesize data;
  22. @synthesize x_labels;
  23. @synthesize y_labels;
  24. - (id)initWithFrame:(CGRect)frame
  25. {
  26. self = [super initWithFrame:frame];
  27. if (self) {
  28. w = frame.size.width;
  29. h = frame.size.height;
  30. //背景色
  31. // self.backgroundColor = RQMianColor;
  32. }
  33. return self;
  34. }
  35. - (void)setData:(NSArray *)_data
  36. {
  37. data = [NSArray arrayWithArray:_data];
  38. // NSString* imgName = @"question_index_bg.png";
  39. CGFloat startTime = .5f;
  40. // UIImageView *iv;
  41. UIColor* lineCol = [UIColor whiteColor];
  42. CGFloat imgw = 12;
  43. CGPoint e;
  44. UIView* ccv;
  45. if (_data.count >0) {
  46. e = [data[0] CGPointValue];
  47. if (max_X == min_X) {
  48. //如果只有一个点 就把点放中间
  49. e.x = w / 2.0;
  50. }else{
  51. e.x = (e.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  52. }
  53. e.y = 0.875*h - (e.y - min_Y)*0.8*h/(max_Y - min_Y);
  54. ccv = [[CCView alloc] initWithFrame:CGRectMake(0, 0, imgw, imgw)];
  55. [self addSubview:ccv];
  56. ccv.center = e;
  57. }
  58. for (int i=1; i<data.count; i++)
  59. {
  60. CGPoint s = [data[i-1] CGPointValue];
  61. CGPoint e = [data[i] CGPointValue];
  62. s.x = (s.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  63. s.y = 0.875*h - (s.y - min_Y)*0.8*h/(max_Y - min_Y);
  64. e.x = (e.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  65. e.y = 0.875*h - (e.y - min_Y)*0.8*h/(max_Y - min_Y);
  66. AFDescribeObj *desObj = [[AFDescribeObj alloc] initWithType:AFAnimationStrokeEnd inView:self];
  67. [desObj setStartTime:startTime andDuration:1.5f/data.count];//用1.5S来画线
  68. [desObj addLineFrom:s toPoint:e color:lineCol];
  69. startTime += 0.5f;
  70. ccv = [[CCView alloc] initWithFrame:CGRectMake(0, 0, imgw, imgw)];
  71. [self addSubview:ccv];
  72. ccv.center = e;
  73. // [ccArr addObject:ccv];
  74. }
  75. // for (UIView* v in ccArr) {
  76. // [self bringSubviewToFront:v];
  77. // }
  78. }
  79. - (void)setX_labels:(NSArray *)_x_labels
  80. {
  81. x_labels = [NSArray arrayWithArray:_x_labels];
  82. CGFloat num = ((CGFloat)x_labels.count)-1.0;
  83. if (num == 0.0) {
  84. num = 2.0f;
  85. }
  86. CGFloat x_interval = 0.8*w/num;
  87. UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0.9*h, w, 0.1*h)];
  88. v.alpha = 0;
  89. for (int i=0; i<x_labels.count; i++)
  90. {
  91. NSString *str = x_labels[i];
  92. if (x_labels.count == 1) {
  93. //如果只有一个点 将label放中间
  94. i ++;
  95. }
  96. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.025*w+i*x_interval, 0, 0.2*w, 0.1*h)];
  97. label.backgroundColor = [UIColor clearColor];
  98. label.textAlignment = NSTextAlignmentCenter;
  99. label.font = [UIFont scaleSize:16.0];
  100. label.text = str;
  101. [v addSubview:label];
  102. [label setTextColor:[UIColor whiteColor]];
  103. }
  104. [self addSubview:v];
  105. [UIView animateWithDuration:1.5f
  106. animations:^{
  107. v.alpha = 1;
  108. }];
  109. }
  110. - (void)setY_labels:(NSArray *)_y_labels
  111. {
  112. y_labels = [NSArray arrayWithArray:_y_labels];
  113. CGFloat num = ((CGFloat)y_labels.count)-1.0;
  114. CGFloat y_interval = 0.8*h/num;
  115. CGFloat wid = .2*w;
  116. UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wid, h)];
  117. v.alpha = 0;
  118. for (int i=0; i<y_labels.count; i++)
  119. {
  120. NSString *str = y_labels[y_labels.count- 1 - i];
  121. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0.875*h - 6 - y_interval*i, wid, w/28.0)];
  122. [label setFrame:CGRectMake(5,h*.78- h/7.0*i, wid, h/7.0)];
  123. label.backgroundColor = [UIColor clearColor];
  124. label.textAlignment = NSTextAlignmentLeft;
  125. label.font = [UIFont scaleSize:16.0];
  126. label.text = str;
  127. [v addSubview:label];
  128. [label setTextColor:[UIColor whiteColor]];
  129. CGFloat hei = h*.95;
  130. UIView*v = [[UIView alloc] initWithFrame:CGRectMake(0.1*w,.08*hei+hei*1.0/y_labels.count*i, w, 1)];
  131. [v setBackgroundColor:RGBA_COLOR(240, 240, 240, 1)];//[UIColor colorWithRed:240 green:240 blue:240 alpha:1]
  132. [self addSubview:v];
  133. label.center = CGPointMake(label.center.x, v.y);
  134. }
  135. [self addSubview:v];
  136. [UIView animateWithDuration:1.5f
  137. animations:^{
  138. v.alpha = 1;
  139. }];
  140. }
  141. @end
  142. @implementation CCView
  143. -(instancetype)initWithFrame:(CGRect)frame
  144. {
  145. self = [super initWithFrame:frame];
  146. if (self) {
  147. [self setBackgroundColor:[UIColor clearColor]];
  148. }
  149. return self;
  150. }
  151. -(void)drawRect:(CGRect)rect
  152. {
  153. //圆的线条宽度
  154. CGFloat lw = 2;
  155. UIColor *color = [UIColor whiteColor];
  156. [color set];//设置线条颜色
  157. CGFloat d = self.frame.size.width * .8;
  158. CGFloat x = (self.frame.size.width - d)/2.0;
  159. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, x, d, d)];
  160. path.lineWidth = lw;
  161. [path stroke];//空心的图形
  162. }
  163. @end