forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTLayerAnnotation.m
More file actions
241 lines (185 loc) · 7.35 KB
/
CPTLayerAnnotation.m
File metadata and controls
241 lines (185 loc) · 7.35 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
#import "CPTLayerAnnotation.h"
#import "CPTAnnotationHostLayer.h"
#import "CPTConstraints.h"
/// @cond
@interface CPTLayerAnnotation()
@property (nonatomic, readwrite, strong) CPTConstraints *xConstraints;
@property (nonatomic, readwrite, strong) CPTConstraints *yConstraints;
-(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 cpt_weak CPTLayer *anchorLayer
* @brief The reference layer.
**/
@synthesize anchorLayer;
/** @property CPTRectAnchor rectAnchor
* @brief The anchor position for the annotation.
**/
@synthesize rectAnchor;
@synthesize xConstraints;
@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.
**/
-(instancetype)initWithAnchorLayer:(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; this will fail the assertion in -initWithAnchorLayer:
-(instancetype)init
{
return [self initWithAnchorLayer:nil];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
anchorLayer = nil;
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(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.
*/
-(instancetype)initWithCoder:(NSCoder *)coder
{
if ( (self = [super init]) ) {
anchorLayer = [coder decodeObjectForKey:@"CPTLayerAnnotation.anchorLayer"];
xConstraints = [coder decodeObjectForKey:@"CPTLayerAnnotation.xConstraints"];
yConstraints = [coder decodeObjectForKey:@"CPTLayerAnnotation.yConstraints"];
rectAnchor = (CPTRectAnchor)[coder decodeIntegerForKey : @"CPTLayerAnnotation.rectAnchor"];
}
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 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];
[self positionContentLayer];
}
}
/// @endcond
@end