-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUIView+Dejal.m
353 lines (262 loc) · 9.25 KB
/
UIView+Dejal.m
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
//
// UIView+Dejal.m
// Dejal Open Source Categories
//
// Created by David Sinclair on 2009-02-04.
// Copyright (c) 2009-2015 Dejal Systems, LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "UIView+Dejal.h"
@import QuartzCore;
@implementation UIView (Dejal)
/**
Returns the receiver’s frame origin.
@author DJS 2009-11.
*/
- (CGPoint)dejal_frameOrigin;
{
return self.frame.origin;
}
/**
Sets the receiver's frame origin, without tedious local variables or hassles.
@author DJS 2009-11.
*/
- (void)dejal_setFrameOrigin:(CGPoint)origin;
{
self.frame = CGRectMake(origin.x, origin.y, self.frame.size.width, self.frame.size.height);
}
/**
Returns the receiver's frame origin X position.
@author DJS 2009-11.
*/
- (CGFloat)dejal_frameX;
{
return self.frame.origin.x;
}
/**
Sets the receiver's frame origin X position, without tedious local variables or hassles.
@author DJS 2009-11.
*/
- (void)dejal_setFrameX:(CGFloat)x;
{
self.dejal_frameOrigin = CGPointMake(x, self.dejal_frameY);
}
/**
Returns the receiver's frame origin Y position.
@author DJS 2009-11.
*/
- (CGFloat)dejal_frameY;
{
return self.frame.origin.y;
}
/**
Sets the receiver's frame origin Y position, without tedious local variables or hassles.
@author DJS 2009-11.
*/
- (void)dejal_setFrameY:(CGFloat)y;
{
self.dejal_frameOrigin = CGPointMake(self.dejal_frameX, y);
}
// ----------------------------------------------------------------------------------------
#pragma mark -
// ----------------------------------------------------------------------------------------
/**
Returns the receiver’s bounds size.
@author DJS 2009-10.
*/
- (CGSize)dejal_boundsSize;
{
return self.bounds.size;
}
/**
Sets the receiver's bounds size, without tedious local variables or hassles.
@author DJS 2009-10.
*/
- (void)dejal_setBoundsSize:(CGSize)size;
{
self.bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, size.width, size.height);
}
/**
Returns the receiver's bounds size width.
@author DJS 2009-10.
*/
- (CGFloat)dejal_boundsWidth;
{
return self.bounds.size.width;
}
/**
Sets the receiver's bounds size width, without tedious local variables or hassles.
@author DJS 2009-10.
*/
- (void)dejal_setBoundsWidth:(CGFloat)width;
{
self.dejal_boundsSize = CGSizeMake(width, self.dejal_boundsHeight);
}
/**
Returns the receiver's bounds size height.
@author DJS 2009-10.
*/
- (CGFloat)dejal_boundsHeight;
{
return self.bounds.size.height;
}
/**
Sets the receiver's bounds size height, without tedious local variables or hassles.
@author DJS 2009-10.
*/
- (void)dejal_setBoundsHeight:(CGFloat)height;
{
self.dejal_boundsSize = CGSizeMake(self.dejal_boundsWidth, height);
}
// ----------------------------------------------------------------------------------------
#pragma mark -
// ----------------------------------------------------------------------------------------
/**
Returns an image representation of the receiver and its subviews.
@author DJS 2009-02.
*/
- (UIImage *)dejal_imageRepresentation;
{
UIGraphicsBeginImageContext(self.frame.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// ----------------------------------------------------------------------------------------
#pragma mark -
// ----------------------------------------------------------------------------------------
/**
Shows or hides the reciever by fading it in or out if animated is YES, or just shows or hides immediately. Uses an animation duration of 0.3, which is the same as the keyboard animation.
@author DJS 2009-10.
*/
- (void)dejal_setHidden:(BOOL)hide animated:(BOOL)animated;
{
[self dejal_setHidden:hide animated:animated withDuration:0.3];
}
/**
Shows or hides the reciever by fading it in or out if animated is YES, or just shows or hides immediately. An animation duration can be specified (ignored if not animating).
@author DJS 2009-10.
*/
- (void)dejal_setHidden:(BOOL)hide animated:(BOOL)animated withDuration:(NSTimeInterval)duration;
{
if (!hide)
{
self.alpha = 0.0;
self.hidden = NO;
}
if (animated)
{
[UIView beginAnimations:@"DejalViewExtrasHiding" context:(__bridge void *)(@(hide))];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:duration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(hideAnimationDidStop:finished:hiding:)];
}
if (!hide)
self.alpha = 1.0;
else if (animated)
self.alpha = 0.0;
else
self.hidden = YES;
if (animated)
[UIView commitAnimations];
}
/**
Hides the receiver once the hide animation has stopped when hiding; does nothing when showing (but is still called, to avoid it getting called mistakenly if the hiding is interrupted by a show).
@author DJS 2009-10.
*/
- (void)hideAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished hiding:(NSNumber *)hide;
{
if ([animationID isEqualToString:@"DejalViewExtrasHiding"] && finished && [hide boolValue] && self.alpha == 0.0)
self.hidden = YES;
}
// ----------------------------------------------------------------------------------------
#pragma mark -
// ----------------------------------------------------------------------------------------
/**
Adds the reciever as a subview of the specified view, optionally fading it in with animation. Uses an animation duration of 0.3, which is the same as the keyboard animation.
@author DJS 2009-10.
*/
- (void)dejal_addToSuperview:(UIView *)view animated:(BOOL)animated;
{
[self dejal_addToSuperview:view animated:animated withDuration:0.3];
}
/**
Adds the reciever as a subview of the specified view, optionally fading it in with animation. An animation duration can be specified (ignored if not animating).
@author DJS 2009-10.
*/
- (void)dejal_addToSuperview:(UIView *)view animated:(BOOL)animated withDuration:(NSTimeInterval)duration;
{
if (animated)
self.alpha = 0.0;
if (!self.superview)
[view addSubview:self];
if (animated)
{
[UIView beginAnimations:@"DejalViewExtrasAddToSuperview" context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:duration];
self.alpha = 1.0;
[UIView commitAnimations];
}
}
/**
Removes the reciever from its superview, optionally fading it out with animation. Uses an animation duration of 0.3, which is the same as the keyboard animation.
@author DJS 2009-10.
*/
- (void)dejal_removeFromSuperviewAnimated:(BOOL)animated;
{
[self dejal_removeFromSuperviewAnimated:animated withDuration:0.3];
}
/**
Removes the reciever from its superview, optionally fading it out with animation. An animation duration can be specified (ignored if not animating).
@author DJS 2009-10.
*/
- (void)dejal_removeFromSuperviewAnimated:(BOOL)animated withDuration:(NSTimeInterval)duration;
{
if (!self.superview)
return;
if (animated)
{
[UIView beginAnimations:@"DejalViewExtrasRemoveFromSuperview" context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:duration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(dejal_removeFromSuperviewAnimationDidStop:finished:context:)];
self.alpha = 0.0;
[UIView commitAnimations];
}
else
[self removeFromSuperview];
}
/**
Removes the receiver from the superview once the hide animation has stopped.
@author DJS 2009-10.
*/
- (void)dejal_removeFromSuperviewAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
{
if ([animationID isEqualToString:@"DejalViewExtrasRemoveFromSuperview"] && finished && self.alpha == 0.0)
[self removeFromSuperview];
}
@end