-
Notifications
You must be signed in to change notification settings - Fork 598
Expand file tree
/
Copy pathCPTAnnotationHostLayer.m
More file actions
338 lines (280 loc) · 9.21 KB
/
CPTAnnotationHostLayer.m
File metadata and controls
338 lines (280 loc) · 9.21 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
#import "CPTAnnotationHostLayer.h"
#import "CPTExceptions.h"
/// @cond
@interface CPTAnnotationHostLayer()
@property (nonatomic, readwrite, strong, nonnull) CPTMutableAnnotationArray *mutableAnnotations;
@end
/// @endcond
#pragma mark -
/** @brief A container layer for annotations.
*
* Annotations (CPTAnnotation) can be added to and removed from an annotation layer.
* The host layer automatically handles the annotation layout.
**/
@implementation CPTAnnotationHostLayer
/** @property nonnull CPTAnnotationArray *annotations
* @brief An array of annotations attached to this layer.
**/
@dynamic annotations;
@synthesize mutableAnnotations;
#pragma mark -
#pragma mark Init/Dealloc
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTAnnotationHostLayer object with the provided frame rectangle.
*
* This is the designated initializer. The initialized layer will have an empty
* @ref annotations array.
*
* @param newFrame The frame rectangle.
* @return The initialized CPTAnnotationHostLayer object.
**/
-(nonnull instancetype)initWithFrame:(CGRect)newFrame
{
if ((self = [super initWithFrame:newFrame])) {
mutableAnnotations = [[NSMutableArray alloc] init];
}
return self;
}
/// @}
/// @cond
-(nonnull instancetype)initWithLayer:(nonnull id)layer
{
if ((self = [super initWithLayer:layer])) {
CPTAnnotationHostLayer *theLayer = (CPTAnnotationHostLayer *)layer;
mutableAnnotations = theLayer->mutableAnnotations;
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:self.mutableAnnotations forKey:@"CPTAnnotationHostLayer.mutableAnnotations"];
}
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ((self = [super initWithCoder:coder])) {
CPTAnnotationArray *annotations = [coder decodeObjectOfClasses:[NSSet setWithArray:@[[NSArray class], [CPTAnnotation class]]]
forKey:@"CPTAnnotationHostLayer.mutableAnnotations"];
if ( annotations ) {
mutableAnnotations = [annotations mutableCopy];
}
else {
mutableAnnotations = [[NSMutableArray alloc] init];
}
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Annotations
/// @cond
-(nonnull CPTAnnotationArray *)annotations
{
return [self.mutableAnnotations copy];
}
/// @endcond
/**
* @brief Adds an annotation to the receiver.
**/
-(void)addAnnotation:(nullable CPTAnnotation *)annotation
{
if ( annotation ) {
CPTAnnotation *theAnnotation = annotation;
CPTMutableAnnotationArray *annotationArray = self.mutableAnnotations;
if ( ![annotationArray containsObject:theAnnotation] ) {
[annotationArray addObject:theAnnotation];
}
theAnnotation.annotationHostLayer = self;
[theAnnotation positionContentLayer];
}
}
/**
* @brief Removes an annotation from the receiver.
**/
-(void)removeAnnotation:(nullable CPTAnnotation *)annotation
{
if ( annotation ) {
CPTAnnotation *theAnnotation = annotation;
if ( [self.mutableAnnotations containsObject:theAnnotation] ) {
theAnnotation.annotationHostLayer = nil;
[self.mutableAnnotations removeObject:theAnnotation];
}
else {
CPTAnnotationHostLayer *hostLayer = theAnnotation.annotationHostLayer;
[NSException raise:CPTException format:@"Tried to remove CPTAnnotation from %@. Host layer was %@.", self, hostLayer];
}
}
}
/**
* @brief Removes all annotations from the receiver.
**/
-(void)removeAllAnnotations
{
CPTMutableAnnotationArray *allAnnotations = self.mutableAnnotations;
for ( CPTAnnotation *annotation in allAnnotations ) {
annotation.annotationHostLayer = nil;
}
[allAnnotations removeAllObjects];
}
#pragma mark -
#pragma mark Layout
/// @cond
-(nullable CPTSublayerSet *)sublayersExcludedFromAutomaticLayout
{
CPTMutableAnnotationArray *annotations = self.mutableAnnotations;
if ( annotations.count > 0 ) {
CPTMutableSublayerSet *excludedSublayers = [super.sublayersExcludedFromAutomaticLayout mutableCopy];
if ( !excludedSublayers ) {
excludedSublayers = [NSMutableSet set];
}
for ( CPTAnnotation *annotation in annotations ) {
CALayer *content = annotation.contentLayer;
if ( content ) {
[excludedSublayers addObject:content];
}
}
return excludedSublayers;
}
else {
return super.sublayersExcludedFromAutomaticLayout;
}
}
-(void)layoutSublayers
{
[super layoutSublayers];
[self.mutableAnnotations makeObjectsPerformSelector:@selector(positionContentLayer)];
}
/// @endcond
#pragma mark -
#pragma mark Event Handling
/// @name User Interaction
/// @{
/**
* @brief Informs the receiver that the user has
* @if MacOnly pressed the mouse button. @endif
* @if iOSOnly touched the screen. @endif
*
*
* The event is passed in turn to each annotation layer that contains the interaction point.
* If any layer handles the event, subsequent layers are not notified and
* this method immediately returns @YES.
*
* @param event The OS event.
* @param interactionPoint The coordinates of the interaction.
* @return Whether the event was handled or not.
**/
-(BOOL)pointingDeviceDownEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
for ( CPTAnnotation *annotation in self.annotations ) {
CPTLayer *content = annotation.contentLayer;
if ( content ) {
if ( CGRectContainsPoint(content.frame, interactionPoint)) {
BOOL handled = [content pointingDeviceDownEvent:event atPoint:interactionPoint];
if ( handled ) {
return YES;
}
}
}
}
return [super pointingDeviceDownEvent:event atPoint:interactionPoint];
}
/**
* @brief Informs the receiver that the user has
* @if MacOnly released the mouse button. @endif
* @if iOSOnly lifted their finger off the screen. @endif
*
*
* The event is passed in turn to each annotation layer that contains the interaction point.
* If any layer handles the event, subsequent layers are not notified and
* this method immediately returns @YES.
*
* @param event The OS event.
* @param interactionPoint The coordinates of the interaction.
* @return Whether the event was handled or not.
**/
-(BOOL)pointingDeviceUpEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
for ( CPTAnnotation *annotation in self.annotations ) {
CPTLayer *content = annotation.contentLayer;
if ( content ) {
if ( CGRectContainsPoint(content.frame, interactionPoint)) {
BOOL handled = [content pointingDeviceUpEvent:event atPoint:interactionPoint];
if ( handled ) {
return YES;
}
}
}
}
return [super pointingDeviceUpEvent:event atPoint:interactionPoint];
}
/**
* @brief Informs the receiver that the user has moved
* @if MacOnly the mouse with the button pressed. @endif
* @if iOSOnly their finger while touching the screen. @endif
*
*
* The event is passed in turn to each annotation layer that contains the interaction point.
* If any layer handles the event, subsequent layers are not notified and
* this method immediately returns @YES.
*
* @param event The OS event.
* @param interactionPoint The coordinates of the interaction.
* @return Whether the event was handled or not.
**/
-(BOOL)pointingDeviceDraggedEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
for ( CPTAnnotation *annotation in self.annotations ) {
CPTLayer *content = annotation.contentLayer;
if ( content ) {
if ( CGRectContainsPoint(content.frame, interactionPoint)) {
BOOL handled = [content pointingDeviceDraggedEvent:event atPoint:interactionPoint];
if ( handled ) {
return YES;
}
}
}
}
return [super pointingDeviceDraggedEvent:event atPoint:interactionPoint];
}
/**
* @brief Informs the receiver that tracking of
* @if MacOnly mouse moves @endif
* @if iOSOnly touches @endif
* has been cancelled for any reason.
*
*
* The event is passed in turn to each annotation layer.
* If any layer handles the event, subsequent layers are not notified and
* this method immediately returns @YES.
*
* @param event The OS event.
* @return Whether the event was handled or not.
**/
-(BOOL)pointingDeviceCancelledEvent:(nonnull CPTNativeEvent *)event
{
for ( CPTAnnotation *annotation in self.annotations ) {
CPTLayer *content = annotation.contentLayer;
if ( content ) {
BOOL handled = [content pointingDeviceCancelledEvent:event];
if ( handled ) {
return YES;
}
}
}
return [super pointingDeviceCancelledEvent:event];
}
/// @}
@end