-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewAdditions.m
328 lines (267 loc) · 7.48 KB
/
UIViewAdditions.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
//
// UIViewAdditions.m
// Weather.IL
//
// Created by Guy Shaviv on 28/3/09.
// Copyright 2009 .. All rights reserved.
//
#import "libAdditions.h"
#import <QuartzCore/QuartzCore.h>
CGSize sizeThatFitsKeepingAspectRatio(CGSize originalSize, CGSize sizeToFit)
{
if (originalSize.width <= sizeToFit.width && originalSize.height <= sizeToFit.height)
{
return originalSize;
}
CGFloat necessaryZoomWidth = sizeToFit.width / originalSize.width;
CGFloat necessaryZoomHeight = sizeToFit.height / originalSize.height;
CGFloat smallerZoom = MIN(necessaryZoomWidth, necessaryZoomHeight);
CGSize scaledSize = CGSizeMake(roundf(originalSize.width*smallerZoom), roundf(originalSize.height*smallerZoom));
return scaledSize;
}
@implementation UIView (Additions)
- (void) setCornerRadius:(NSNumber*)rad {
self.layer.cornerRadius = [rad floatValue];
}
- (void) setBorderWidth:(NSNumber*)width {
self.layer.borderWidth = [width floatValue];
}
- (void) setBorderColor:(id)webColor {
if ([webColor isKindOfClass:[UIColor class]]) {
self.layer.borderColor = [webColor CGColor];
} else {
self.layer.borderColor = [UIColor colorWithHTMLName:webColor].CGColor;
}
}
- (void) setShadowOffset:(CGSize)offset {
self.layer.shadowOffset = offset;
}
- (void) setShadowColor:(UIColor*)webColor {
if ([webColor isKindOfClass:[NSString class]]) {
webColor = [UIColor colorWithHTMLName:(id)webColor];
}
self.layer.shadowColor =webColor.CGColor;
}
- (void) setShadowOpacity:(NSString*)number {
self.layer.shadowOpacity = [number floatValue];
}
- (void) setShadowRadius:(NSNumber*)number {
self.layer.shadowRadius = [number floatValue];
}
- (void) setResizable:(NSString*)insets {
if ([self isKindOfClass:[UIImageView class]]) {
UIImageView *iself = (UIImageView*)self;
UIEdgeInsets caps = UIEdgeInsetsFromString(insets);
iself.image = [iself.image resizableImageWithCapInsets:caps];
}
}
- (CGFloat)left {
return self.frame.origin.x;
}
- (void)setLeft:(CGFloat)x {
if (x != x) return;
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)top {
return self.frame.origin.y;
}
- (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
}
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)height {
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)screenX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left;
}
return x;
}
- (CGFloat)screenY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top;
}
return y;
}
- (CGFloat)screenViewX {
CGFloat x = 0;
for (UIView* view = self; view; view = view.superview) {
x += view.left;
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
x -= scrollView.contentOffset.x;
}
}
return x;
}
- (CGFloat)screenViewY {
CGFloat y = 0;
for (UIView* view = self; view; view = view.superview) {
y += view.top;
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView* scrollView = (UIScrollView*)view;
y -= scrollView.contentOffset.y;
}
}
return y;
}
- (CGPoint)offsetFromView:(UIView*)otherView {
CGFloat x = 0, y = 0;
for (UIView* view = self; view && view != otherView; view = view.superview) {
x += view.left;
y += view.top;
}
return CGPointMake(x, y);
}
- (UIScrollView*)findFirstScrollView {
if ([self isKindOfClass:[UIScrollView class]])
return (UIScrollView*)self;
for (UIView* child in self.subviews) {
UIScrollView* it = [child findFirstScrollView];
if (it)
return it;
}
return nil;
}
- (UIView*)firstViewOfClass:(Class)cls {
if ([self isKindOfClass:cls])
return self;
for (UIView* child in self.subviews) {
UIView* it = [child firstViewOfClass:cls];
if (it)
return it;
}
return nil;
}
- (UIView*)firstParentOfClass:(Class)cls {
if ([self isKindOfClass:cls]) {
return self;
} else if (self.superview) {
return [self.superview firstParentOfClass:cls];
} else {
return nil;
}
}
- (UIView*)findChildWithDescendant:(UIView*)descendant {
for (UIView* view = descendant; view && view != self; view = view.superview) {
if (view.superview == self) {
return view;
}
}
return nil;
}
- (void) removeSubviews {
[[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
- (NSInteger) indexOfView:(UIView*)view {
NSArray *subviews = self.subviews;
for (int i=0;i<subviews.count;i++) {
if ([subviews[i] isEqual:view]) {
return i;
}
}
return -1;
}
- (UIViewController*)viewController {
for (UIView* next = self; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}
- (UIView *)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
if ([subView isFirstResponder]){
return subView;
}
UIView *fr = [subView findFirstResponder];
if (fr){
return fr;
}
}
return nil;
}
- (void) setAngle:(NSNumber*)degrees {
self.transform = CGAffineTransformMakeRotation([degrees floatValue]/180.*M_PI);
}
- (void) makeScrollViewGestureRecognizersRequireFail:(UIGestureRecognizer*)toFail {
if ([self isKindOfClass:[UIScrollView class]]) {
[(UIScrollView*)self makeGestureRecognizersRequireFail:toFail];
}
for (UIView *item in self.subviews) {
[item makeScrollViewGestureRecognizersRequireFail:toFail];
}
}
- (void) makeScrollViewGestureRecognizersRequireViewToFail:(UIView*)toFail {
if ([self isKindOfClass:[UIScrollView class]]) {
for (UIGestureRecognizer *rec in toFail.gestureRecognizers)
[(UIScrollView*)self makeGestureRecognizersRequireFail:rec];
}
for (UIView *item in self.subviews) {
[item makeScrollViewGestureRecognizersRequireViewToFail:toFail];
}
}
@end
#ifdef QUARTZADDITIONS
@implementation UIView (QuartzAdditions)
- (UIImage*) renderToImage {
float scale = self.layer.contentsScale;
UIGraphicsBeginImageContextWithOptions(self.bounds.size,YES,scale);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
- (void)shakeWithOffset:(CGFloat)aOffset damping:(CGFloat)damping duration:(CGFloat)aDuration maxShakes:(NSInteger)maxShakes {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[animation setDuration:aDuration];
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:20];
while(aOffset > 0.01) {
[keys addObject:[NSValue valueWithCGPoint:CGPointMake(self.center.x - aOffset, self.center.y)]];
aOffset *= damping;
[keys addObject:[NSValue valueWithCGPoint:CGPointMake(self.center.x + aOffset, self.center.y)]];
aOffset *= damping;
maxShakes--;
if(maxShakes <= 0) {
break;
}
}
animation.values = keys;
[self.layer addAnimation:animation forKey:@"position"];
}
- (void)shake {
[self shakeWithOffset:40.0 damping:0.85 duration:1.5 maxShakes:30];
}
@end
#endif