-
Notifications
You must be signed in to change notification settings - Fork 2
/
KPViewPositioningManager.j
199 lines (170 loc) · 7.64 KB
/
KPViewPositioningManager.j
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
// KPViewPositioningManager.j
// View Controllers
//
// Created by Klaas Pieter Annema on 19/02/2010.
// Copyright (c) 2010 Klaas Pieter Annema
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
@import <Foundation/CPObject.j>
var KPViewPositioningManagerInstance = nil;
/*!
KPViewPositioningManager is a helper class providing often used view placement code.
*/
@implementation KPViewPositioningManager : CPObject
{
}
/*!
Returns the singleton instance of the positioning manager.
@return The default positioning manager.
*/
+ (id)defaultManager
{
if (!KPViewPositioningManagerInstance)
KPViewPositioningManagerInstance = [[self alloc] init];
return KPViewPositioningManagerInstance;
}
/*!
Places a view inside another view with no inset.
@param theSubview the view to be placed inside theSuperview
@param theSuperview the view theSubview will be added to
@see placeView:inView:inset:
*/
- (void)placeView:(CPView)theSubview inView:(CPView)theSuperview
{
[self placeView:theSubview inView:theSuperview inset:CGInsetMakeZero()];
}
/*!
Places a view inside another view with the specified inset. Will throw an exception if any of the views are nil or
if the views are equal.
@param theSubview the view to be placed inside theSuperview, theSubview may not be nil.
@param theSuperview the view theSubview is added to, theSuperview may not be nil.
@param theInset the inset of theSubview relative to theSuperview's bounds
*/
- (void)placeView:(CPView)theSubview inView:(CPView)theSuperview inset:(CGInset)theInset
{
if (!theSuperview)
[CPException raise:CPInternalInconsistencyException reason:@"KPViewPositioningManager placeView:inView: superview must not be nil"];
if (!theSubview)
[CPException raise:CPInternalInconsistencyException reason:@"KPViewPositioningManager placeView:inView: subview must not be nil"];
if (theSubview === theSuperview)
[CPException raise:CPInternalInconsistencyException reason:@"KPViewPositioningManager placeView:inView: subview must not be equal to superview"];
var bounds = [theSuperview bounds];
bounds.origin.x += theInset.left;
bounds.origin.y += theInset.top;
bounds.size.width -= theInset.left + theInset.right;
bounds.size.height -= theInset.top + theInset.bottom;
[theSubview setFrame:bounds];
[theSuperview addSubview:theSubview];
}
/*!
Places a view in the center of another view.
@param theSubview the view to be placed in the center
@param theSuperview the view theSubview is added to
*/
- (void)placeView:(CPView)theSubview inCenterOfView:(CPView)theSuperview
{
var bounds = [theSuperview bounds],
frame = [theSubview frame];
frame.origin.x = CGRectGetMidX(bounds) - (CGRectGetWidth(frame) / 2.0);
frame.origin.y = CGRectGetMidY(bounds) - (CGRectGetHeight(frame) / 2.0);
[theSubview setFrame:frame];
[theSuperview addSubview:theSubview];
}
/*!
Places a view left of another view with no offset.
@param theLeftView The view that is placed relative to theRightView
@param theRightView The view theLeftView is placed relative to
@see placeView:leftOfView:offset:
*/
- (void)placeView:(CPView)theLeftView leftOfView:(CPView)theRightView
{
[self placeView:theLeftView leftOfView:theRightView offset:CGPointMakeZero()];
}
/*!
Places a view left of another view at the specified offset.
@param theLeftView The view that is placed relative to theRightView
@param theRightView The view theLeftView is placed relative to
@param theOffset The offset of theLeftView relative to theRightView
*/
- (void)placeView:(CPView)theLeftView leftOfView:(CPView)theRightView offset:(CGPoint)theOffset
{
if ([theRightView superview] && [theLeftView superview] && [theLeftView superview] !== [theRightView superview])
CPLog.warn(@"%@ the views do not have the same superview", CPStringFromSelector(_cmd));
var leftFrame = [theLeftView frame],
rightFrame = [theRightView frame];
leftFrame.origin.x = CGRectGetMinX(rightFrame) - CGRectGetWidth(leftFrame) + theOffset.x;
leftFrame.origin.y = CGRectGetMinY(rightFrame) + theOffset.y;
[theLeftView setFrame:leftFrame];
}
/*!
Places a view right of another view with no offset.
@param theRightView The view that is placed relative to theLeftView
@param theLeftView The view theRightView is placed relative to
@see placeView:rightOfView:offset:
*/
- (void)placeView:(CPView)theRightView rightOfView:(CPView)theLeftView
{
[self placeView:theRightView rightOfView:theLeftView offset:CPPointMakeZero()];
}
/*!
Places a view right of another view at the specified offset.
@param theRightView The view that is placed relative to theLeftView
@param theLeftView The view theRightView is placed relative to
@param theOffset The offset of theRightView relative to theLeftView
*/
- (void)placeView:(CPView)theRightView rightOfView:(CPView)theLeftView offset:(CPPoint)theOffset
{
if ([theRightView superview] && [theLeftView superview] && [theLeftView superview] !== [theRightView superview])
CPLog.warn(@"%@ the views do not have the same superview", CPStringFromSelector(_cmd));
var leftFrame = [theLeftView frame],
rightFrame = [theRightView frame];
rightFrame.origin.x = CPRectGetMaxX(leftFrame) + theOffset.x;
rightFrame.origin.y = CPRectGetMinY(leftFrame) + theOffset.y;
[theRightView setFrame:rightFrame];
}
/*!
Places a view below another view with no offset.
@param theBelowView The view that is placed relative to theAboveView
@param theAboveView The view theBelowView is placed relative to
@param theOffset The offset of theBelowView relative to theAboveView
*/
- (void)placeView:(CPView)theBelowView belowView:(CPView)theAboveView
{
[self placeView:theBelowView belowView:theAboveView offset:CPPointMakeZero()];
}
/*!
Places a view below another view at the specified offset.
@param theBelowView The view to place below the other view
@param theAboveView The view to place the below view relative of
@param theOffset The offset of the below relative to the above view
*/
- (void)placeView:(CPView)theBelowView belowView:(CPView)theAboveView offset:(CPPoint)theOffset
{
if ([theBelowView superview] && [theAboveView superview] && [theBelowView superview] !== [theAboveView superview])
CPLog.warn(@"%@ the views do not have the same superview", CPStringFromSelector(_cmd));
var aboveFrame = [theAboveView frame],
belowFrame = [theBelowView frame];
belowFrame.origin.x = CPRectGetMinX(aboveFrame) + theOffset.x;
belowFrame.origin.y = CPRectGetMaxY(aboveFrame) + theOffset.y;
[theBelowView setFrame:belowFrame];
}
@end