-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphView.m
511 lines (426 loc) · 15.6 KB
/
GraphView.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#import "GraphView.h"
#pragma mark - Overview of operation
// The GraphView class needs to be able to update the scene quickly in order to track the accelerometer data
// at a fast enough frame rate. The naive implementation tries to draw the entire graph every frame,
// but unfortunately that is too much content to sustain a high framerate. As such this class uses CALayers
// to cache previously drawn content and arranges them carefully to create an illusion that we are
// redrawing the entire graph every frame.
#pragma mark - Quartz Helpers
// Functions used to draw all content
CGColorRef CreateDeviceGrayColor(CGFloat w, CGFloat a)
{
CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray();
CGFloat comps[] = {w, a};
CGColorRef color = CGColorCreate(gray, comps);
CGColorSpaceRelease(gray);
return color;
}
CGColorRef CreateDeviceRGBColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
{
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat comps[] = {r, g, b, a};
CGColorRef color = CGColorCreate(rgb, comps);
CGColorSpaceRelease(rgb);
return color;
}
CGColorRef graphBackgroundColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceGrayColor(0.6, 1.0);
}
return c;
}
CGColorRef graphLineColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceGrayColor(0.5, 1.0);
}
return c;
}
CGColorRef graphXColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(1.0, 0.0, 0.0, 1.0);
}
return c;
}
CGColorRef graphYColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(0.0, 1.0, 0.0, 1.0);
}
return c;
}
CGColorRef graphZColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(0.0, 0.0, 1.0, 1.0);
}
return c;
}
void DrawGridlines(CGContextRef context, CGFloat x, CGFloat width)
{
for(CGFloat y = -48.5; y <= 48.5; y += 16.0)
{
CGContextMoveToPoint(context, x, y);
CGContextAddLineToPoint(context, x + width, y);
}
CGContextSetStrokeColorWithColor(context, graphLineColor());
CGContextStrokePath(context);
}
#pragma mark - GraphViewSegment
// The GraphViewSegment manages up to 32 accelerometer values and a CALayer that it updates with
// the segment of the graph that those values represent.
@interface GraphViewSegment : NSObject
{
CALayer *layer;
// Need 33 values to fill 32 pixel width.
UIAccelerationValue xhistory[33];
UIAccelerationValue yhistory[33];
UIAccelerationValue zhistory[33];
int index;
}
// returns true if adding this value fills the segment, which is necessary for properly updating the segments
-(BOOL)addX:(UIAccelerationValue)x y:(UIAccelerationValue)y z:(UIAccelerationValue)z;
// When this object gets recycled (when it falls off the end of the graph)
// -reset is sent to clear values and prepare for reuse.
-(void)reset;
// Returns true if this segment has consumed 32 values.
-(BOOL)isFull;
// Returns true if the layer for this segment is visible in the given rect.
-(BOOL)isVisibleInRect:(CGRect)r;
// The layer that this segment is drawing into
@property(nonatomic, readonly) CALayer *layer;
@end
@implementation GraphViewSegment
@synthesize layer;
-(id)init
{
self = [super init];
if(self != nil)
{
layer = [[CALayer alloc] init];
// the layer will call our -drawLayer:inContext: method to provide content
// and our -actionForLayer:forKey: for implicit animations
layer.delegate = self;
// This sets our coordinate system such that it has an origin of 0.0,-56 and a size of 32,112.
// This would need to be changed if you change either the number of pixel values that a segment
// represented, or if you changed the size of the graph view.
layer.bounds = CGRectMake(0.0, -56.0, 32.0, 112.0);
// Disable blending as this layer consists of non-transperant content.
// Unlike UIView, a CALayer defaults to opaque=NO
layer.opaque = YES;
// Index represents how many slots are left to be filled in the graph,
// which is also +1 compared to the array index that a new entry will be added
index = 33;
}
return self;
}
-(void)reset
{
// Clear out our components and reset the index to 33 to start filling values again...
memset(xhistory, 0, sizeof(xhistory));
memset(yhistory, 0, sizeof(yhistory));
memset(zhistory, 0, sizeof(zhistory));
index = 33;
// Inform Core Animation that we need to redraw this layer.
[layer setNeedsDisplay];
}
-(BOOL)isFull
{
// Simple, this segment is full if there are no more space in the history.
return index == 0;
}
-(BOOL)isVisibleInRect:(CGRect)r
{
// Just check if there is an intersection between the layer's frame and the given rect.
return CGRectIntersectsRect(r, layer.frame);
}
-(BOOL)addX:(UIAccelerationValue)x y:(UIAccelerationValue)y z:(UIAccelerationValue)z
{
// If this segment is not full, then we add a new acceleration value to the history.
if(index > 0)
{
// First decrement, both to get to a zero-based index and to flag one fewer position left
--index;
xhistory[index] = x;
yhistory[index] = y;
zhistory[index] = z;
// And inform Core Animation to redraw the layer.
[layer setNeedsDisplay];
}
// And return if we are now full or not (really just avoids needing to call isFull after adding a value).
return index == 0;
}
-(void)drawLayer:(CALayer*)l inContext:(CGContextRef)context
{
// Fill in the background
CGContextSetFillColorWithColor(context, graphBackgroundColor());
CGContextFillRect(context, layer.bounds);
// Draw the grid lines
DrawGridlines(context, 0.0, 32.0);
// Draw the graph
CGPoint lines[64];
int i;
// X
for(i = 0; i < 32; ++i)
{
lines[i*2].x = i;
lines[i*2].y = -xhistory[i] * 16.0;
lines[i*2+1].x = i + 1;
lines[i*2+1].y = -xhistory[i+1] * 16.0;
}
CGContextSetStrokeColorWithColor(context, graphXColor());
CGContextStrokeLineSegments(context, lines, 64);
// Y
for(i = 0; i < 32; ++i)
{
lines[i*2].y = -yhistory[i] * 16.0;
lines[i*2+1].y = -yhistory[i+1] * 16.0;
}
CGContextSetStrokeColorWithColor(context, graphYColor());
CGContextStrokeLineSegments(context, lines, 64);
// Z
for(i = 0; i < 32; ++i)
{
lines[i*2].y = -zhistory[i] * 16.0;
lines[i*2+1].y = -zhistory[i+1] * 16.0;
}
CGContextSetStrokeColorWithColor(context, graphZColor());
CGContextStrokeLineSegments(context, lines, 64);
}
-(id)actionForLayer:(CALayer *)layer forKey :(NSString *)key
{
// We disable all actions for the layer, so no content cross fades, no implicit animation on moves, etc.
return [NSNull null];
}
// The accessibilityValue of this segment should be the x,y,z values last added.
- (NSString *)accessibilityValue
{
return [NSString stringWithFormat:NSLocalizedString(@"graphSegmentFormat", @""), xhistory[index], yhistory[index], zhistory[index]];
}
@end
#pragma mark - GraphTextView
// We use a seperate view to draw the text for the graph so that we can layer the segment layers below it
// which gives the illusion that the numbers are draw over the graph, and hides the fact that the graph drawing
// for each segment is incomplete until the segment is filled.
@interface GraphTextView : UIView
{
}
@end
@implementation GraphTextView
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Fill in the background
CGContextSetFillColorWithColor(context, graphBackgroundColor());
CGContextFillRect(context, self.bounds);
CGContextTranslateCTM(context, 0.0, 56.0);
// Draw the grid lines
DrawGridlines(context, 26.0, 6.0);
// Draw the text
UIFont *systemFont = [UIFont systemFontOfSize:12.0];
[[UIColor whiteColor] set];
[@"+3.0" drawInRect:CGRectMake(2.0, -56.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@"+2.0" drawInRect:CGRectMake(2.0, -40.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@"+1.0" drawInRect:CGRectMake(2.0, -24.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@" 0.0" drawInRect:CGRectMake(2.0, -8.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@"-1.0" drawInRect:CGRectMake(2.0, 8.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@"-2.0" drawInRect:CGRectMake(2.0, 24.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
[@"-3.0" drawInRect:CGRectMake(2.0, 40.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
}
@end
#pragma mark - GraphView
// Finally the actual GraphView class. This class handles the public interface as well as arranging
// the subviews and sublayers to produce the intended effect.
@interface GraphView()
// Internal accessors
@property(nonatomic) NSMutableArray *segments;
@property(nonatomic, unsafe_unretained) GraphViewSegment *current;
@property(nonatomic) GraphTextView *text;
// A common init routine for use with -initWithFrame: and -initWithCoder:
-(void)commonInit;
// Creates a new segment, adds it to 'segments', and returns a weak reference to that segment
// Typically a graph will have around a dozen segments, but this depends on the width of the graph view and segments
-(GraphViewSegment*)addSegment;
// Recycles a segment from 'segments' into 'current'
-(void)recycleSegment;
@end
@implementation GraphView
@synthesize segments, current, text;
// Designated initializer
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self != nil)
{
[self commonInit];
}
return self;
}
// Designated initializer
-(id)initWithCoder:(NSCoder*)decoder
{
self = [super initWithCoder:decoder];
if(self != nil)
{
[self commonInit];
}
return self;
}
-(void)commonInit
{
// Create the text view and add it as a subview. We keep a weak reference
// to that view afterwards for laying out the segment layers.
text = [[GraphTextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 32.0, 112.0)];
[self addSubview:text];
// Create a mutable array to store segments, which is required by -addSegment
segments = [[NSMutableArray alloc] init];
// Create a new current segment, which is required by -addX:y:z and other methods.
// This is also a weak reference (we assume that the 'segments' array will keep the strong reference).
current = [self addSegment];
self.backgroundColor = [UIColor clearColor];
}
-(void)addX:(UIAccelerationValue)x y:(UIAccelerationValue)y z:(UIAccelerationValue)z
{
// First, add the new acceleration value to the current segment
if([current addX:x y:y z:z])
{
// If after doing that we've filled up the current segment, then we need to
// determine the next current segment
[self recycleSegment];
// And to keep the graph looking continuous, we add the acceleration value to the new segment as well.
[current addX:x y:y z:z];
}
// After adding a new data point, we need to advance the x-position of all the segment layers by 1 to
// create the illusion that the graph is advancing.
for(GraphViewSegment * s in segments)
{
CGPoint position = s.layer.position;
position.x += 1.0;
s.layer.position = position;
}
}
-(void)addRotationX:(float)x y:(float)y z:(float)z{
// First, add the new acceleration value to the current segment
// we normalize the data so it can fit in the screen
x = x / SCALE_FACTOR;
y = y / SCALE_FACTOR;
z = z / SCALE_FACTOR;
if([current addX:x y:y z:z])
{
// If after doing that we've filled up the current segment, then we need to
// determine the next current segment
[self recycleSegment];
// And to keep the graph looking continuous, we add the acceleration value to the new segment as well.
[current addX:x y:y z:z];
}
// After adding a new data point, we need to advance the x-position of all the segment layers by 1 to
// create the illusion that the graph is advancing.
for(GraphViewSegment * s in segments)
{
CGPoint position = s.layer.position;
position.x += 1.0;
s.layer.position = position;
}
}
-(void)addAtttidueX:(float)x y:(float)y z:(float)z{
// First, add the new acceleration value to the current segment
if([current addX:x y:y z:z])
{
// If after doing that we've filled up the current segment, then we need to
// determine the next current segment
[self recycleSegment];
// And to keep the graph looking continuous, we add the acceleration value to the new segment as well.
[current addX:x y:y z:z];
}
// After adding a new data point, we need to advance the x-position of all the segment layers by 1 to
// create the illusion that the graph is advancing.
for(GraphViewSegment * s in segments)
{
CGPoint position = s.layer.position;
position.x += 1.0;
s.layer.position = position;
}
}
// The initial position of a segment that is meant to be displayed on the left side of the graph.
// This positioning is meant so that a few entries must be added to the segment's history before it becomes
// visible to the user. This value could be tweaked a little bit with varying results, but the X coordinate
// should never be larger than 16 (the center of the text view) or the zero values in the segment's history
// will be exposed to the user.
#define kSegmentInitialPosition CGPointMake(14.0, 56.0);
-(GraphViewSegment*)addSegment
{
// Create a new segment and add it to the segments array.
GraphViewSegment * segment = [[GraphViewSegment alloc] init];
// We add it at the front of the array because -recycleSegment expects the oldest segment
// to be at the end of the array. As long as we always insert the youngest segment at the front
// this will be true.
[segments insertObject:segment atIndex:0];
// this is now a weak reference
// Ensure that newly added segment layers are placed after the text view's layer so that the text view
// always renders above the segment layer.
[self.layer insertSublayer:segment.layer below:text.layer];
// Position it properly (see the comment for kSegmentInitialPosition)
segment.layer.position = kSegmentInitialPosition;
return segment;
}
-(void)recycleSegment
{
// We start with the last object in the segments array, as it should either be visible onscreen,
// which indicates that we need more segments, or pushed offscreen which makes it eligable for recycling.
GraphViewSegment * last = [segments lastObject];
if([last isVisibleInRect:self.layer.bounds])
{
// The last segment is still visible, so create a new segment, which is now the current segment
current = [self addSegment];
}
else
{
// The last segment is no longer visible, so we reset it in preperation to be recycled.
[last reset];
// Position it properly (see the comment for kSegmentInitialPosition)
last.layer.position = kSegmentInitialPosition;
// Move the segment from the last position in the array to the first position in the array
// as it is now the youngest segment.
[segments insertObject:last atIndex:0];
[segments removeLastObject];
// And make it our current segment
current = last;
}
}
// The graph view itself exists only to draw the background and gridlines. All other content is drawn either into
// the GraphTextView or into a layer managed by a GraphViewSegment.
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Fill in the background
CGContextSetFillColorWithColor(context, graphBackgroundColor());
CGContextFillRect(context, self.bounds);
CGFloat width = self.bounds.size.width;
CGContextTranslateCTM(context, 0.0, 56.0);
// Draw the grid lines
DrawGridlines(context, 0.0, width);
}
// Return an up-to-date value for the graph.
- (NSString *)accessibilityValue
{
if (segments.count == 0)
{
return nil;
}
// Let the GraphViewSegment handle its own accessibilityValue;
GraphViewSegment *graphViewSegment = [segments objectAtIndex:0];
return [graphViewSegment accessibilityValue];
}
@end