forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTGraphHostingView.m
More file actions
424 lines (327 loc) · 11.5 KB
/
Copy pathCPTGraphHostingView.m
File metadata and controls
424 lines (327 loc) · 11.5 KB
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
#import "CPTGraphHostingView.h"
#import "CPTGraph.h"
#import "CPTPlotArea.h"
#import "CPTPlotAreaFrame.h"
#import "CPTPlotSpace.h"
#import "NSNumberExtensions.h"
/// @cond
@interface CPTGraphHostingView()
@property (nonatomic, readwrite, nullable, cpt_weak_property) cpt_weak UIPinchGestureRecognizer *pinchGestureRecognizer;
-(void)graphNeedsRedraw:(nonnull NSNotification *)notification;
-(void)handlePinchGesture:(nonnull UIPinchGestureRecognizer *)aPinchGestureRecognizer;
@end
/// @endcond
#pragma mark -
/**
* @brief A container view for displaying a CPTGraph.
**/
@implementation CPTGraphHostingView
/** @property nullable CPTGraph *hostedGraph
* @brief The CPTLayer hosted inside this view.
**/
@synthesize hostedGraph;
/** @property BOOL collapsesLayers
* @brief Whether view draws all graph layers into a single layer.
* Collapsing layers may improve performance in some cases.
**/
@synthesize collapsesLayers;
/** @property BOOL allowPinchScaling
* @brief Whether a pinch will trigger plot space scaling.
* Default is @YES. This causes gesture recognizers to be added to identify pinches.
**/
@synthesize allowPinchScaling;
/// @cond
/** @internal
* @property nullable cpt_weak UIPinchGestureRecognizer *pinchGestureRecognizer
* @brief The pinch gesture recognizer for this view.
**/
@synthesize pinchGestureRecognizer;
/// @endcond
#pragma mark -
#pragma mark init/dealloc
/// @cond
+(nonnull Class)layerClass
{
return [CALayer class];
}
-(void)commonInit
{
self.hostedGraph = nil;
self.collapsesLayers = NO;
self.backgroundColor = [UIColor clearColor];
self.allowPinchScaling = YES;
// This undoes the normal coordinate space inversion that UIViews apply to their layers
self.layer.sublayerTransform = CATransform3DMakeScale( CPTFloat(1.0), CPTFloat(-1.0), CPTFloat(1.0) );
}
-(nonnull instancetype)initWithFrame:(CGRect)frame
{
if ( (self = [super initWithFrame:frame]) ) {
[self commonInit];
}
return self;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/// @endcond
#pragma mark -
#pragma mark NSCoding methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeBool:self.collapsesLayers forKey:@"CPTGraphHostingView.collapsesLayers"];
[coder encodeObject:self.hostedGraph forKey:@"CPTGraphHostingView.hostedGraph"];
[coder encodeBool:self.allowPinchScaling forKey:@"CPTGraphHostingView.allowPinchScaling"];
// No need to archive these properties:
// pinchGestureRecognizer
}
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ( (self = [super initWithCoder:coder]) ) {
[self commonInit];
collapsesLayers = [coder decodeBoolForKey:@"CPTGraphHostingView.collapsesLayers"];
self.hostedGraph = [coder decodeObjectForKey:@"CPTGraphHostingView.hostedGraph"]; // setup layers
if ( [coder containsValueForKey:@"CPTGraphHostingView.allowPinchScaling"] ) {
self.allowPinchScaling = [coder decodeBoolForKey:@"CPTGraphHostingView.allowPinchScaling"]; // set gesture recognizer if needed
}
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark Touch handling
/// @cond
-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
BOOL handled = NO;
// Ignore pinch or other multitouch gestures
if ( [event allTouches].count == 1 ) {
CPTGraph *theHostedGraph = self.hostedGraph;
UIEvent *theEvent = event;
theHostedGraph.frame = self.bounds;
[theHostedGraph layoutIfNeeded];
CGPoint pointOfTouch = [[[theEvent touchesForView:self] anyObject] locationInView:self];
if ( self.collapsesLayers ) {
pointOfTouch.y = self.frame.size.height - pointOfTouch.y;
}
else {
pointOfTouch = [self.layer convertPoint:pointOfTouch toLayer:theHostedGraph];
}
handled = [theHostedGraph pointingDeviceDownEvent:theEvent atPoint:pointOfTouch];
}
if ( !handled ) {
[super touchesBegan:touches withEvent:event];
}
}
-(void)touchesMoved:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
BOOL handled = NO;
if ( event ) {
CPTGraph *theHostedGraph = self.hostedGraph;
UIEvent *theEvent = event;
theHostedGraph.frame = self.bounds;
[theHostedGraph layoutIfNeeded];
CGPoint pointOfTouch = [[[theEvent touchesForView:self] anyObject] locationInView:self];
if ( self.collapsesLayers ) {
pointOfTouch.y = self.frame.size.height - pointOfTouch.y;
}
else {
pointOfTouch = [self.layer convertPoint:pointOfTouch toLayer:theHostedGraph];
}
handled = [theHostedGraph pointingDeviceDraggedEvent:theEvent atPoint:pointOfTouch];
}
if ( !handled ) {
[super touchesMoved:touches withEvent:event];
}
}
-(void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
BOOL handled = NO;
if ( event ) {
CPTGraph *theHostedGraph = self.hostedGraph;
UIEvent *theEvent = event;
theHostedGraph.frame = self.bounds;
[theHostedGraph layoutIfNeeded];
CGPoint pointOfTouch = [[[theEvent touchesForView:self] anyObject] locationInView:self];
if ( self.collapsesLayers ) {
pointOfTouch.y = self.frame.size.height - pointOfTouch.y;
}
else {
pointOfTouch = [self.layer convertPoint:pointOfTouch toLayer:theHostedGraph];
}
handled = [theHostedGraph pointingDeviceUpEvent:theEvent atPoint:pointOfTouch];
}
if ( !handled ) {
[super touchesEnded:touches withEvent:event];
}
}
-(void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
BOOL handled = NO;
if ( event ) {
UIEvent *theEvent = event;
handled = [self.hostedGraph pointingDeviceCancelledEvent:theEvent];
}
if ( !handled ) {
[super touchesCancelled:touches withEvent:event];
}
}
/// @endcond
#pragma mark -
#pragma mark Gestures
/// @cond
-(void)setAllowPinchScaling:(BOOL)allowScaling
{
if ( allowPinchScaling != allowScaling ) {
allowPinchScaling = allowScaling;
if ( allowPinchScaling ) {
// Register for pinches
UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[self addGestureRecognizer:gestureRecognizer];
self.pinchGestureRecognizer = gestureRecognizer;
}
else {
UIPinchGestureRecognizer *pinchRecognizer = self.pinchGestureRecognizer;
if ( pinchRecognizer ) {
[self removeGestureRecognizer:pinchRecognizer];
self.pinchGestureRecognizer = nil;
}
}
}
}
-(void)handlePinchGesture:(nonnull UIPinchGestureRecognizer *)aPinchGestureRecognizer
{
CGPoint interactionPoint = [aPinchGestureRecognizer locationInView:self];
CPTGraph *theHostedGraph = self.hostedGraph;
theHostedGraph.frame = self.bounds;
[theHostedGraph layoutIfNeeded];
if ( self.collapsesLayers ) {
interactionPoint.y = self.frame.size.height - interactionPoint.y;
}
else {
interactionPoint = [self.layer convertPoint:interactionPoint toLayer:theHostedGraph];
}
CGPoint pointInPlotArea = [theHostedGraph convertPoint:interactionPoint toLayer:theHostedGraph.plotAreaFrame.plotArea];
UIPinchGestureRecognizer *pinchRecognizer = self.pinchGestureRecognizer;
CGFloat scale = pinchRecognizer.scale;
for ( CPTPlotSpace *space in theHostedGraph.allPlotSpaces ) {
if ( space.allowsUserInteraction ) {
[space scaleBy:scale aboutPoint:pointInPlotArea];
}
}
pinchRecognizer.scale = 1.0;
}
/// @endcond
#pragma mark -
#pragma mark Drawing
/// @cond
-(void)drawRect:(CGRect)rect
{
if ( self.collapsesLayers ) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1, -1);
CPTGraph *theHostedGraph = self.hostedGraph;
theHostedGraph.frame = self.bounds;
[theHostedGraph layoutAndRenderInContext:context];
}
}
-(void)graphNeedsRedraw:(nonnull NSNotification *)notification
{
[self setNeedsDisplay];
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setHostedGraph:(nullable CPTGraph *)newLayer
{
NSParameterAssert( (newLayer == nil) || [newLayer isKindOfClass:[CPTGraph class]] );
if ( newLayer == hostedGraph ) {
return;
}
if ( hostedGraph ) {
[hostedGraph removeFromSuperlayer];
hostedGraph.hostingView = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:CPTGraphNeedsRedrawNotification
object:hostedGraph];
}
hostedGraph = newLayer;
// Screen scaling
UIScreen *screen = self.window.screen;
if ( !screen ) {
screen = [UIScreen mainScreen];
}
hostedGraph.contentsScale = screen.scale;
hostedGraph.hostingView = self;
if ( self.collapsesLayers ) {
[self setNeedsDisplay];
if ( hostedGraph ) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(graphNeedsRedraw:)
name:CPTGraphNeedsRedrawNotification
object:hostedGraph];
}
}
else {
if ( newLayer ) {
CPTGraph *newGraph = newLayer;
newGraph.frame = self.layer.bounds;
[self.layer addSublayer:newGraph];
}
}
}
-(void)setCollapsesLayers:(BOOL)collapse
{
if ( collapse != collapsesLayers ) {
collapsesLayers = collapse;
CPTGraph *theHostedGraph = self.hostedGraph;
[self setNeedsDisplay];
if ( collapsesLayers ) {
[theHostedGraph removeFromSuperlayer];
if ( theHostedGraph ) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(graphNeedsRedraw:)
name:CPTGraphNeedsRedrawNotification
object:theHostedGraph];
}
}
else {
if ( theHostedGraph ) {
[self.layer addSublayer:theHostedGraph];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:CPTGraphNeedsRedrawNotification
object:theHostedGraph];
}
}
}
}
-(void)setFrame:(CGRect)newFrame
{
super.frame = newFrame;
CPTGraph *theHostedGraph = self.hostedGraph;
[theHostedGraph setNeedsLayout];
if ( self.collapsesLayers ) {
[self setNeedsDisplay];
}
else {
theHostedGraph.frame = self.bounds;
}
}
-(void)setBounds:(CGRect)newBounds
{
super.bounds = newBounds;
CPTGraph *theHostedGraph = self.hostedGraph;
[theHostedGraph setNeedsLayout];
if ( self.collapsesLayers ) {
[self setNeedsDisplay];
}
else {
theHostedGraph.frame = newBounds;
}
}
/// @endcond
@end