AFView.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. self.backgroundColor = defGreen;
  31. }
  32. return self;
  33. }
  34. - (void)setData:(NSArray *)_data
  35. {
  36. data = [NSArray arrayWithArray:_data];
  37. // NSString* imgName = @"question_index_bg.png";
  38. CGFloat startTime = .5f;
  39. // UIImageView *iv;
  40. UIColor* lineCol = [UIColor whiteColor];
  41. CGFloat imgw = 12;
  42. CGPoint e;
  43. UIView* ccv;
  44. if (_data.count >0) {
  45. e = [data[0] CGPointValue];
  46. e.x = (e.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  47. e.y = 0.875*h - (e.y - min_Y)*0.8*h/(max_Y - min_Y);
  48. ccv = [[CCView alloc] initWithFrame:CGRectMake(0, 0, imgw, imgw)];
  49. [self addSubview:ccv];
  50. ccv.center = e;
  51. }
  52. for (int i=1; i<data.count; i++)
  53. {
  54. CGPoint s = [data[i-1] CGPointValue];
  55. CGPoint e = [data[i] CGPointValue];
  56. s.x = (s.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  57. s.y = 0.875*h - (s.y - min_Y)*0.8*h/(max_Y - min_Y);
  58. e.x = (e.x - min_X)*0.8*w/(max_X - min_X) + 0.125*w;
  59. e.y = 0.875*h - (e.y - min_Y)*0.8*h/(max_Y - min_Y);
  60. AFDescribeObj *desObj = [[AFDescribeObj alloc] initWithType:AFAnimationStrokeEnd inView:self];
  61. [desObj setStartTime:startTime andDuration:0.5f];
  62. [desObj addLineFrom:s toPoint:e color:lineCol];
  63. startTime += 0.5f;
  64. ccv = [[CCView alloc] initWithFrame:CGRectMake(0, 0, imgw, imgw)];
  65. [self addSubview:ccv];
  66. ccv.center = e;
  67. // [ccArr addObject:ccv];
  68. }
  69. // for (UIView* v in ccArr) {
  70. // [self bringSubviewToFront:v];
  71. // }
  72. }
  73. - (void)setX_labels:(NSArray *)_x_labels
  74. {
  75. x_labels = [NSArray arrayWithArray:_x_labels];
  76. CGFloat num = ((CGFloat)x_labels.count)-1.0;
  77. CGFloat x_interval = 0.8*w/num;
  78. UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0.9*h, w, 0.1*h)];
  79. v.alpha = 0;
  80. for (int i=0; i<x_labels.count; i++)
  81. {
  82. NSString *str = x_labels[i];
  83. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.025*w+i*x_interval, 0, 0.2*w, 0.1*h)];
  84. label.backgroundColor = [UIColor clearColor];
  85. label.textAlignment = NSTextAlignmentCenter;
  86. label.font = [UIFont scaleSize:16.0];
  87. label.text = str;
  88. [v addSubview:label];
  89. [label setTextColor:[UIColor whiteColor]];
  90. }
  91. [self addSubview:v];
  92. [UIView animateWithDuration:1.5f
  93. animations:^{
  94. v.alpha = 1;
  95. }];
  96. }
  97. - (void)setY_labels:(NSArray *)_y_labels
  98. {
  99. y_labels = [NSArray arrayWithArray:_y_labels];
  100. CGFloat num = ((CGFloat)y_labels.count)-1.0;
  101. CGFloat y_interval = 0.8*h/num;
  102. CGFloat wid = .2*w;
  103. UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wid, h)];
  104. v.alpha = 0;
  105. for (int i=0; i<y_labels.count; i++)
  106. {
  107. NSString *str = y_labels[y_labels.count- 1 - i];
  108. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0.875*h - 6 - y_interval*i, wid, w/28.0)];
  109. [label setFrame:CGRectMake(5,h*.78- h/7.0*i, wid, h/7.0)];
  110. label.backgroundColor = [UIColor clearColor];
  111. label.textAlignment = NSTextAlignmentLeft;
  112. label.font = [UIFont scaleSize:16.0];
  113. label.text = str;
  114. [v addSubview:label];
  115. [label setTextColor:[UIColor whiteColor]];
  116. CGFloat hei = h*.95;
  117. UIView*v = [[UIView alloc] initWithFrame:CGRectMake(0.1*w,.08*hei+hei*1.0/y_labels.count*i, w, 1)];
  118. [v setBackgroundColor:RGBA_COLOR(240, 240, 240, 1)];//[UIColor colorWithRed:240 green:240 blue:240 alpha:1]
  119. [self addSubview:v];
  120. label.center = CGPointMake(label.center.x, v.y);
  121. }
  122. [self addSubview:v];
  123. [UIView animateWithDuration:1.5f
  124. animations:^{
  125. v.alpha = 1;
  126. }];
  127. }
  128. @end
  129. @implementation CCView
  130. -(instancetype)initWithFrame:(CGRect)frame
  131. {
  132. self = [super initWithFrame:frame];
  133. if (self) {
  134. [self setBackgroundColor:[UIColor clearColor]];
  135. }
  136. return self;
  137. }
  138. -(void)drawRect:(CGRect)rect
  139. {
  140. //圆的线条宽度
  141. CGFloat lw = 2;
  142. UIColor *color = [UIColor whiteColor];
  143. [color set];//设置线条颜色
  144. CGFloat d = self.frame.size.width * .8;
  145. CGFloat x = (self.frame.size.width - d)/2.0;
  146. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, x, d, d)];
  147. path.lineWidth = lw;
  148. [path stroke];//空心的图形
  149. }
  150. @end