-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+Shadow.m
executable file
·85 lines (67 loc) · 3.52 KB
/
UIView+Shadow.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
//
// UIView+Shadow.m
// Shadow Maker Example
//
// Created by Philip Yu on 5/14/13.
// Copyright (c) 2013 Philip Yu. All rights reserved.
//
#import "UIView+Shadow.h"
#define kShadowViewTag 2132
#define kValidDirections [NSArray arrayWithObjects: @"top", @"bottom", @"left", @"right",nil]
@implementation UIView (Shadow)
- (void) makeInsetShadow {
NSArray *shadowDirections = [NSArray arrayWithObjects:@"top", @"bottom", @"left" , @"right" , nil];
UIColor *color = [UIColor colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:0.5];
UIView *shadowView = [self createShadowViewWithRadius:3 Color:color Directions:shadowDirections];
shadowView.tag = kShadowViewTag;
[self addSubview:shadowView];
}
- (void) makeInsetShadowWithRadius:(float)radius Alpha:(float)alpha {
NSArray *shadowDirections = [NSArray arrayWithObjects:@"top", @"bottom", @"left" , @"right" , nil];
UIColor *color = [UIColor colorWithRed:(0.0) green:(0.0) blue:(0.0) alpha:alpha];
UIView *shadowView = [self createShadowViewWithRadius:radius Color:color Directions:shadowDirections];
shadowView.tag = kShadowViewTag;
[self addSubview:shadowView];
}
- (void) makeInsetShadowWithRadius:(float)radius Color:(UIColor *)color Directions:(NSArray *)directions {
UIView *shadowView = [self createShadowViewWithRadius:radius Color:color Directions:directions];
shadowView.tag = kShadowViewTag;
[self addSubview:shadowView];
}
- (UIView *) createShadowViewWithRadius:(float)radius Color:(UIColor *)color Directions:(NSArray *)directions {
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
shadowView.backgroundColor = [UIColor clearColor];
// Ignore duplicate direction
NSMutableDictionary *directionDict = [[NSMutableDictionary alloc] init];
for (NSString *direction in directions) [directionDict setObject:@"1" forKey:direction];
for (NSString *direction in directionDict) {
// Ignore invalid direction
if ([kValidDirections containsObject:direction]) {
CAGradientLayer *shadow = [CAGradientLayer layer];
if ([direction isEqualToString:@"top"]) {
[shadow setStartPoint:CGPointMake(0.5, 0.0)];
[shadow setEndPoint:CGPointMake(0.5, 1.0)];
shadow.frame = CGRectMake(0, 0, self.bounds.size.width, radius);
}
else if ([direction isEqualToString:@"bottom"]) {
[shadow setStartPoint:CGPointMake(0.5, 1.0)];
[shadow setEndPoint:CGPointMake(0.5, 0.0)];
shadow.frame = CGRectMake(0, self.bounds.size.height - radius, self.bounds.size.width, radius);
}
else if ([direction isEqualToString:@"left"]) {
shadow.frame = CGRectMake(0, 0, radius, self.bounds.size.height);
[shadow setStartPoint:CGPointMake(0.0, 0.5)];
[shadow setEndPoint:CGPointMake(1.0, 0.5)];
}
else if ([direction isEqualToString:@"right"]) {
shadow.frame = CGRectMake(self.bounds.size.width - radius, 0, radius, self.bounds.size.height);
[shadow setStartPoint:CGPointMake(1.0, 0.5)];
[shadow setEndPoint:CGPointMake(0.0, 0.5)];
}
shadow.colors = [NSArray arrayWithObjects:(id)[color CGColor], (id)[[UIColor clearColor] CGColor], nil];
[shadowView.layer insertSublayer:shadow atIndex:0];
}
}
return shadowView;
}
@end