-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLSegmentedView.m
executable file
·252 lines (188 loc) · 7.05 KB
/
CLSegmentedView.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
//
// CLSegmentedView.m
// Cascade
//
// Created by Emil Wojtaszek on 11-04-24.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CLSegmentedView.h"
@interface CLSegmentedView (Private)
- (void) setupViews;
- (void) updateRoundedCorners;
@end
@implementation CLSegmentedView
@synthesize footerView = _footerView;
@synthesize headerView = _headerView;
@synthesize contentView = _contentView;
@synthesize shadowWidth = _shadowWidth;
@synthesize viewSize = _viewSize;
@synthesize showRoundedCorners = _showRoundedCorners;
@synthesize rectCorner = _rectCorner;
@synthesize shadowOffset = _shadowOffset;
#pragma mark - Init & dealloc
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init
{
self = [super init];
if (self) {
_roundedCornersView = [[UIView alloc] init];
[_roundedCornersView setBackgroundColor: [UIColor clearColor]];
[self addSubview: _roundedCornersView];
_viewSize = CLViewSizeNormal;
_rectCorner = UIRectCornerAllCorners;
_showRoundedCorners = NO;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id) initWithSize:(CLViewSize)size {
self = [self init];
if (self) {
_viewSize = size;
}
return self;
}
#pragma mark -
#pragma mark Setters
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setContentView:(UIView*)contentView {
if (_contentView != contentView) {
[_contentView removeFromSuperview];
_contentView = contentView;
if (_contentView) {
[_contentView setAutoresizingMask:
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
[_roundedCornersView addSubview: _contentView];
[self setNeedsLayout];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setHeaderView:(UIView*)headerView {
if (_headerView != headerView) {
[_headerView removeFromSuperview];
_headerView = headerView;
if (_headerView) {
[_headerView setAutoresizingMask:
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin];
[_headerView setUserInteractionEnabled:YES];
[_roundedCornersView addSubview: _headerView];
[self setNeedsLayout];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setFooterView:(UIView*)footerView {
if (_footerView != footerView) {
[_footerView removeFromSuperview];
_footerView = footerView;
if (_footerView) {
[_footerView setAutoresizingMask:
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin];
[_footerView setUserInteractionEnabled:YES];
[_roundedCornersView addSubview: _footerView];
[self setNeedsLayout];
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) addLeftBorderShadowView:(UIView *)view withWidth:(CGFloat)width {
[self setClipsToBounds: NO];
if (_shadowWidth != width) {
_shadowWidth = width;
[self setNeedsLayout];
[self setNeedsDisplay];
}
if (view != _shadowView) {
_shadowView = view;
[self insertSubview:_shadowView atIndex:0];
[self setNeedsLayout];
[self setNeedsDisplay];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) removeLeftBorderShadowView {
[self setClipsToBounds: YES];
_shadowView = nil;
[self setNeedsLayout];
}
#pragma mark -
#pragma mark Private
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) updateRoundedCorners {
if (_showRoundedCorners) {
CGRect toolbarBounds = self.bounds;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect: toolbarBounds
byRoundingCorners:_rectCorner
cornerRadii:CGSizeMake(6.0f, 6.0f)];
[maskLayer setPath:[path CGPath]];
_roundedCornersView.layer.masksToBounds = YES;
_roundedCornersView.layer.mask = maskLayer;
}
else {
_roundedCornersView.layer.masksToBounds = NO;
[_roundedCornersView.layer setMask: nil];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void) layoutSubviews {
CGRect rect = self.bounds;
CGFloat viewWidth = rect.size.width;
CGFloat viewHeight = rect.size.height;
CGFloat headerHeight = 0.0;
CGFloat footerHeight = 0.0;
_roundedCornersView.frame = rect;
if (_headerView) {
headerHeight = _headerView.frame.size.height;
CGRect newHeaderViewFrame = CGRectMake(0.0, 0.0, viewWidth, headerHeight);
[_headerView setFrame: newHeaderViewFrame];
}
if (_footerView) {
footerHeight = _footerView.frame.size.height;
CGFloat footerY = viewHeight - footerHeight;
CGRect newFooterViewFrame = CGRectMake(0.0, footerY, viewWidth, footerHeight);
[_footerView setFrame: newFooterViewFrame];
}
[_contentView setFrame: CGRectMake(0.0, headerHeight, viewWidth, viewHeight - headerHeight - footerHeight)];
if (_shadowView) {
CGRect shadowFrame = CGRectMake(0 - _shadowWidth + _shadowOffset, 0.0, _shadowWidth, rect.size.height);
_shadowView.frame = shadowFrame;
}
[self updateRoundedCorners];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc
{
_footerView = nil;
_headerView = nil;
_contentView = nil;
_roundedCornersView = nil;
_shadowView = nil;
}
#pragma mark
#pragma mark Setters
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setRectCorner:(UIRectCorner)corners {
if (corners != _rectCorner) {
_rectCorner = corners;
[self setNeedsLayout];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) setShowRoundedCorners:(BOOL)show {
if (show != _showRoundedCorners) {
_showRoundedCorners = show;
[self setNeedsLayout];
}
}
@end