-
Notifications
You must be signed in to change notification settings - Fork 72
/
HeartLive.m
140 lines (111 loc) · 3.68 KB
/
HeartLive.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// HeartLive.m
// HeartBeatsPlugin
//
// Created by A053 on 16/9/9.
// Copyright © 2016年 Yvan. All rights reserved.
//
#import "HeartLive.h"
@interface HeartLive ()
@property (strong, nonatomic) NSMutableArray *points;
@end
static CGFloat grid_w = 30.0f;
@implementation HeartLive
- (void)drawRateWithPoint:(NSNumber *)point {
// 倒叙插入数组
[self.points insertObject:point atIndex:0];
// 删除溢出屏幕数据
if (self.points.count > self.frame.size.width/6) {
[self.points removeLastObject];
}
dispatch_async(dispatch_get_main_queue(), ^{
// 这个方法自动调取 drawRect:方法
[self setNeedsDisplay];
});
}
- (void)drawRate {
CGFloat ww = self.frame.size.width;
CGFloat hh = self.frame.size.height;
CGFloat pos_x = ww;
CGFloat pos_y = hh/2;
// 获取当前画布
CGContextRef context = UIGraphicsGetCurrentContext();
// 折线宽度
CGContextSetLineWidth(context, 1.0);
//消除锯齿
//CGContextSetAllowsAntialiasing(context,false);
// 折线颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, pos_x, pos_y);
for (int i = 0; i < self.points.count; i++) {
float h = [self.points[i] floatValue];
pos_y = hh/2 + (h * hh/2) ;
CGContextAddLineToPoint(context, pos_x, pos_y);
pos_x -=6;
}
CGContextStrokePath(context);
}
#pragma mark - 网格
- (void)buildGrid {
CGFloat wight = self.frame.size.width;
CGFloat height = self.frame.size.height;
// 获取当前画布
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat pos_x = 0.0f;
CGFloat pos_y = 0.0f;
// 在wight范围内画竖线
while (pos_x < wight) {
// 设置网格线宽度
CGContextSetLineWidth(context, 0.2);
// 设置网格线颜色
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
// 起点
CGContextMoveToPoint(context, pos_x, 1.0f);
// 终点
CGContextAddLineToPoint(context, pos_x, height);
pos_x +=grid_w;
//开始划线
CGContextStrokePath(context);
}
// 在height范围内画横线
while (pos_y < height) {
CGContextSetLineWidth(context, 0.2);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextMoveToPoint(context, 1.0f, pos_y);
CGContextAddLineToPoint(context, wight, pos_y);
pos_y +=grid_w;
CGContextStrokePath(context);
}
pos_x = 0.0f; pos_y = 0.0f;
// 在wight范围内画竖线
while (pos_x < wight) {
CGContextSetLineWidth(context, 0.1);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextMoveToPoint(context, pos_x, 1.0f);
CGContextAddLineToPoint(context, pos_x, height);
pos_x +=grid_w/5;
CGContextStrokePath(context);
}
// 在height范围内画横线
while (pos_y < height) {
CGContextSetLineWidth(context, 0.1);
CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
CGContextMoveToPoint(context, 1.0f, pos_y);
CGContextAddLineToPoint(context, wight, pos_y);
pos_y +=grid_w/5;
CGContextStrokePath(context);
}
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame: frame]) {
self.backgroundColor = [UIColor blackColor];
self.points = [[NSMutableArray alloc]init];
self.clearsContextBeforeDrawing = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self buildGrid];
[self drawRate];
}
@end