-
Notifications
You must be signed in to change notification settings - Fork 598
Expand file tree
/
Copy pathCPTLayerAnnotation.m
More file actions
286 lines (222 loc) · 8.85 KB
/
CPTLayerAnnotation.m
File metadata and controls
286 lines (222 loc) · 8.85 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
#import "CPTLayerAnnotation.h"
#import "CPTAnnotationHostLayer.h"
#import "CPTConstraints.h"
#import "CPTExceptions.h"
/// @cond
@interface CPTLayerAnnotation()
-(void)setConstraints;
@end
/// @endcond
#pragma mark -
/** @brief Positions a content layer relative to an anchor point in a reference layer.
*
* Layer annotations are positioned relative to a reference layer. This allows the
* annotation content layer to move with changes in the reference layer.
* This is useful for applications such as titles attached to an edge of the reference layer.
**/
@implementation CPTLayerAnnotation
/** @property nullable CPTLayer *anchorLayer
* @brief The reference layer.
**/
@synthesize anchorLayer;
/** @property CPTRectAnchor rectAnchor
* @brief The anchor position for the annotation.
**/
@synthesize rectAnchor;
/** @property CPTConstraints *xConstraints
* @brief The constraints used to position the content layer relative to the reference layer in the x-direction. Setting the @ref rectAnchor resets the constraints.
**/
@synthesize xConstraints;
/** @property CPTConstraints *yConstraints
* @brief The constraints used to position the content layer relative to the reference layer in the y-direction. Setting the @ref rectAnchor resets the constraints.
**/
@synthesize yConstraints;
#pragma mark -
#pragma mark Init/Dealloc
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTLayerAnnotation object with the provided reference layer.
*
* This is the designated initializer. The initialized layer will be anchored to
* #CPTRectAnchorTop by default.
*
* @param newAnchorLayer The reference layer. Must be non-@nil.
* @return The initialized CPTLayerAnnotation object.
**/
-(nonnull instancetype)initWithAnchorLayer:(nonnull CPTLayer *)newAnchorLayer
{
NSParameterAssert(newAnchorLayer);
if ((self = [super init])) {
anchorLayer = newAnchorLayer;
rectAnchor = CPTRectAnchorTop;
xConstraints = nil;
yConstraints = nil;
[self setConstraints];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionContentLayer)
name:CPTLayerBoundsDidChangeNotification
object:newAnchorLayer];
}
return self;
}
/// @}
/// @cond
// anchorLayer is required
-(nonnull instancetype)init
{
[NSException raise:CPTException format:@"%@ must be initialized with an anchor layer.", NSStringFromClass([self class])];
return [self initWithAnchorLayer:[CPTLayer layer]];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
anchorLayer = nil;
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeConditionalObject:self.anchorLayer forKey:@"CPTLayerAnnotation.anchorLayer"];
[coder encodeObject:self.xConstraints forKey:@"CPTLayerAnnotation.xConstraints"];
[coder encodeObject:self.yConstraints forKey:@"CPTLayerAnnotation.yConstraints"];
[coder encodeInteger:self.rectAnchor forKey:@"CPTLayerAnnotation.rectAnchor"];
}
/// @endcond
/** @brief Returns an object initialized from data in a given unarchiver.
* @param coder An unarchiver object.
* @return An object initialized from data in a given unarchiver.
*/
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ((self = [super initWithCoder:coder])) {
CPTLayer *anchor = [coder decodeObjectOfClass:[CPTLayer class]
forKey:@"CPTLayerAnnotation.anchorLayer"];
xConstraints = [coder decodeObjectOfClass:[CPTConstraints class]
forKey:@"CPTLayerAnnotation.xConstraints"];
yConstraints = [coder decodeObjectOfClass:[CPTConstraints class]
forKey:@"CPTLayerAnnotation.yConstraints"];
rectAnchor = (CPTRectAnchor)[coder decodeIntegerForKey:@"CPTLayerAnnotation.rectAnchor"];
if ( anchor ) {
anchorLayer = anchor;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(positionContentLayer)
name:CPTLayerBoundsDidChangeNotification
object:anchor];
}
}
return self;
}
#pragma mark -
#pragma mark Layout
/// @cond
-(void)positionContentLayer
{
CPTLayer *content = self.contentLayer;
if ( content ) {
CPTAnnotationHostLayer *hostLayer = self.annotationHostLayer;
if ( hostLayer ) {
CPTLayer *theAnchorLayer = self.anchorLayer;
CGRect anchorLayerBounds = theAnchorLayer.bounds;
CGFloat xPosition = [self.xConstraints positionForLowerBound:CGRectGetMinX(anchorLayerBounds)
upperBound:CGRectGetMaxX(anchorLayerBounds)];
CGFloat yPosition = [self.yConstraints positionForLowerBound:CGRectGetMinY(anchorLayerBounds)
upperBound:CGRectGetMaxY(anchorLayerBounds)];
CGPoint referencePoint = CPTPointMake(xPosition, yPosition);
CGPoint newPosition = [theAnchorLayer convertPoint:referencePoint toLayer:hostLayer];
CGPoint offset = self.displacement;
newPosition.x += offset.x;
newPosition.y += offset.y;
content.anchorPoint = self.contentAnchorPoint;
content.position = newPosition;
content.transform = CATransform3DMakeRotation(self.rotation, CPTFloat(0.0), CPTFloat(0.0), CPTFloat(1.0));
[content pixelAlign];
}
}
}
/// @endcond
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Constraints
/// @cond
-(void)setConstraints
{
CPTConstraints *xConstraint = nil;
CPTConstraints *yConstraint = nil;
switch ( self.rectAnchor ) {
case CPTRectAnchorRight:
xConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
break;
case CPTRectAnchorTopRight:
xConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorTop:
xConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
yConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorTopLeft:
xConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorLeft:
xConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
break;
case CPTRectAnchorBottomLeft:
xConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorBottom:
xConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
yConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorBottomRight:
xConstraint = [[CPTConstraints alloc] initWithUpperOffset:CPTFloat(0.0)];
yConstraint = [[CPTConstraints alloc] initWithLowerOffset:CPTFloat(0.0)];
break;
case CPTRectAnchorCenter:
xConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
yConstraint = [[CPTConstraints alloc] initWithRelativeOffset:CPTFloat(0.5)];
break;
}
self.xConstraints = xConstraint;
self.yConstraints = yConstraint;
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setRectAnchor:(CPTRectAnchor)newAnchor
{
if ( newAnchor != rectAnchor ) {
rectAnchor = newAnchor;
[self setConstraints];
}
}
-(void)setXConstraints:(CPTConstraints *)newConstraints
{
if ( newConstraints != xConstraints ) {
xConstraints = newConstraints;
[self positionContentLayer];
}
}
-(void)setYConstraints:(CPTConstraints *)newConstraints
{
if ( newConstraints != yConstraints ) {
yConstraints = newConstraints;
[self positionContentLayer];
}
}
/// @endcond
@end