forked from steipete/PSFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSShadowView.m
154 lines (128 loc) · 4.24 KB
/
PSShadowView.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
//
// PSShadowView.m
// PSFoundation
//
// Created by Peter Steinberger on 09.11.10.
// Copyright 2010 Peter Steinberger. All rights reserved.
//
#import "PSShadowView.h"
#define SHADOW_HEIGHT 20.0
#define SHADOW_INVERSE_HEIGHT 10.0
#define SHADOW_RATIO (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT)
@interface PSShadowView ()
@property (nonatomic, retain) CAGradientLayer *topShadowLayer;
@property (nonatomic, retain) CAGradientLayer *bottomShadowLayer;
@end
@implementation PSShadowView
@synthesize topShadowLayer = _topShadowLayer;
@synthesize bottomShadowLayer = _bottomShadowLayer;
@synthesize topShadow = _topShadow;
@synthesize bottomShadow = _bottomShadow;
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark static
+ (PSShadowView *)viewWithSubview:(UIView *)view {
PSShadowView *shadowView = [[[[self class] alloc] init] autorelease];
shadowView.topShadow = YES;
shadowView.bounds = view.bounds;
shadowView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[shadowView addSubview:view];
return shadowView;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark private
// borrowed from ShadowTableView
- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse {
CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
CGRect newShadowFrame =
CGRectMake(0, 0, self.frame.size.width,
inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
newShadow.frame = newShadowFrame;
CGColorRef darkColor =
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.6 : 0.6].CGColor;
CGColorRef lightColor = [UIColor clearColor].CGColor;
//[self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
newShadow.colors =
[NSArray arrayWithObjects:
(id)(inverse ? lightColor : darkColor),
(id)(inverse ? darkColor : lightColor),
nil];
return newShadow;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark NSObject
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
- (void)dealloc {
MCRelease(_topShadowLayer);
MCRelease(_bottomShadowLayer);
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIView
- (void)setFrame:(CGRect)rect {
[super setFrame:rect];
[self setNeedsDisplay];
}
// layoutSubviews
//
// Override to layout the shadows
//
- (void)layoutSubviews {
[super layoutSubviews];
if (self.isTopShadow) {
if (!self.topShadowLayer)
{
self.topShadowLayer = [self shadowAsInverse:YES];
[self.layer addSublayer:self.topShadowLayer];
}
else if ([self.layer.sublayers indexOfObjectIdenticalTo:self.topShadowLayer] != 0)
{
[self.layer addSublayer:self.topShadowLayer];
}
CGRect shadowFrame = self.topShadowLayer.frame;
shadowFrame.size.width = self.frame.size.width;
shadowFrame.origin.y = -SHADOW_INVERSE_HEIGHT;
self.topShadowLayer.frame = shadowFrame;
}else {
[self.topShadowLayer removeFromSuperlayer];
self.topShadowLayer = nil;
}
if (self.isBottomShadow) {
if (!self.bottomShadowLayer)
{
self.bottomShadowLayer = [self shadowAsInverse:NO];
[self.layer insertSublayer:self.bottomShadowLayer atIndex:0];
}
else if ([self.layer.sublayers indexOfObjectIdenticalTo:self.bottomShadowLayer] != 0)
{
[self.layer insertSublayer:self.bottomShadowLayer atIndex:0];
}
CGRect shadowFrame = self.bottomShadowLayer.frame;
shadowFrame.size.width = self.frame.size.width;
shadowFrame.origin.y = self.frame.size.height;
self.bottomShadowLayer.frame = shadowFrame;
}else {
[self.bottomShadowLayer removeFromSuperlayer];
self.bottomShadowLayer = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Properties
- (void)setTopShadow:(BOOL)flag {
_topShadow = flag;
[self setNeedsLayout];
}
- (void)setBottomShadow:(BOOL)flag {
_bottomShadow = flag;
[self setNeedsLayout];
}
@end