This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathILTranslucentView.m
executable file
·298 lines (224 loc) · 8.94 KB
/
ILTranslucentView.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
//
// ILTranslucentView.m
// ILTranslucentViewProject
//
// Created by Ivo Leko on 10/11/13.
// Copyright (c) 2013 Ivo Leko. All rights reserved.
//
#import "ILTranslucentView.h"
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface ILTranslucentView () {
UIView *nonexistentSubview; //this is first child view of ILTranslucentView. It is not available trough "Managing the View Hierarchy" methods.
UIView *toolbarContainerClipView; //view which contain UIToolbar as child subview
UIToolbar *toolBarBG; //this is empty toolbar which we use to produce blur (translucent) effect
UIView *overlayBackgroundView; //view over toolbar that is responsive to backgroundColor property
BOOL initComplete;
}
@property (nonatomic, copy) UIColor *ilColorBG; //backGround color
@property (nonatomic, copy) UIColor *ilDefaultColorBG; //default Apple's color of UIToolbar
@end
@implementation ILTranslucentView
@synthesize translucentAlpha = _translucentAlpha;
#pragma mark - Initalization
- (id)initWithFrame:(CGRect)frame //code
{
self = [super initWithFrame:frame];
if (self) {
[self createUI];
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder { //XIB
self = [super initWithCoder:aDecoder];
if (self) {
[self createUI];
}
return self;
}
- (void) createUI {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
_ilColorBG = self.backgroundColor; //get background color of view (can be set trough interface builder before)
self.ilDefaultColorBG=toolBarBG.barTintColor; //Apple's default background color
_translucent=YES;
_translucentAlpha=1;
// creating nonexistentSubview
nonexistentSubview = [[UIView alloc] initWithFrame:self.bounds];
nonexistentSubview.backgroundColor=[UIColor clearColor];
nonexistentSubview.clipsToBounds=YES;
nonexistentSubview.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
[self insertSubview:nonexistentSubview atIndex:0];
//creating toolbarContainerClipView
toolbarContainerClipView = [[UIView alloc] initWithFrame:self.bounds];
toolbarContainerClipView.backgroundColor=[UIColor clearColor];
toolbarContainerClipView.clipsToBounds=YES;
toolbarContainerClipView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
[nonexistentSubview addSubview:toolbarContainerClipView];
//creating toolBarBG
//we must clip 1px line on the top of toolbar
CGRect rect= self.bounds;
rect.origin.y-=1;
rect.size.height+=1;
toolBarBG =[[UIToolbar alloc] initWithFrame:rect];
toolBarBG.frame=rect;
toolBarBG.autoresizingMask= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[toolbarContainerClipView addSubview:toolBarBG];
//view above toolbar, great for changing blur color effect
overlayBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
overlayBackgroundView.backgroundColor = self.backgroundColor;
overlayBackgroundView.autoresizingMask= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[toolbarContainerClipView addSubview:overlayBackgroundView];
[self setBackgroundColor:[UIColor clearColor]]; //view must be transparent :)
initComplete=YES;
}
}
#pragma mark - Configuring a View’s Visual Appearance
- (BOOL) isItClearColor: (UIColor *) color {
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha =0.0;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
if (red!=0 || green != 0 || blue != 0 || alpha != 0) {
return NO;
}
else {
return YES;
}
}
- (void) setFrame:(CGRect)frame {
// - Setting frame of view -
// UIToolbar's frame is not great at animating. It produces lot of glitches.
// Because of that, we never actually reduce size of toolbar"
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
CGRect rect = frame;
rect.origin.x=0;
rect.origin.y=0;
if (toolbarContainerClipView.frame.size.width>rect.size.width) {
rect.size.width=toolbarContainerClipView.frame.size.width;
}
if (toolbarContainerClipView.frame.size.height>rect.size.height) {
rect.size.height=toolbarContainerClipView.frame.size.height;
}
toolbarContainerClipView.frame=rect;
[super setFrame:frame];
[nonexistentSubview setFrame:self.bounds];
}
else
[super setFrame:frame];
}
- (void) setBounds:(CGRect)bounds {
// - Setting bounds of view -
// UIToolbar's bounds is not great at animating. It produces lot of glitches.
// Because of that, we never actually reduce size of toolbar"
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
CGRect rect = bounds;
rect.origin.x=0;
rect.origin.y=0;
if (toolbarContainerClipView.bounds.size.width>rect.size.width) {
rect.size.width=toolbarContainerClipView.bounds.size.width;
}
if (toolbarContainerClipView.bounds.size.height>rect.size.height) {
rect.size.height=toolbarContainerClipView.bounds.size.height;
}
toolbarContainerClipView.bounds=rect;
[super setBounds:bounds];
[nonexistentSubview setFrame:self.bounds];
}
else
[super setBounds:bounds];
}
- (void) setTranslucentStyle:(UIBarStyle)translucentStyle {
toolBarBG.barStyle=translucentStyle;
}
- (UIBarStyle) translucentStyle {
return toolBarBG.barStyle;
}
- (void) setTranslucentTintColor:(UIColor *)translucentTintColor {
_translucentTintColor = translucentTintColor;
//tint color of toolbar
if ([self isItClearColor:translucentTintColor])
[toolBarBG setBarTintColor:self.ilDefaultColorBG];
else
[toolBarBG setBarTintColor:translucentTintColor];
}
- (void) setBackgroundColor:(UIColor *)backgroundColor {
//changing backgroundColor of view actually change tintColor of toolbar
if (initComplete) {
self.ilColorBG=backgroundColor;
if (_translucent) {
overlayBackgroundView.backgroundColor = backgroundColor;
[super setBackgroundColor:[UIColor clearColor]];
}
else {
[super setBackgroundColor:self.ilColorBG];
}
}
else
[super setBackgroundColor:backgroundColor];
}
- (void) setTranslucent:(BOOL)translucent {
//enabling and disabling blur effect
_translucent=translucent;
toolBarBG.translucent=translucent;
if (translucent) {
toolBarBG.hidden=NO;
[toolBarBG setBarTintColor:self.ilColorBG];
[self setBackgroundColor:[UIColor clearColor]];
}
else {
toolBarBG.hidden=YES;
[self setBackgroundColor:self.ilColorBG];
}
}
- (void) setTranslucentAlpha:(CGFloat)translucentAlpha {
//changing alpha of toolbar
if (translucentAlpha>1)
_translucentAlpha=1;
else if (translucentAlpha<0)
_translucentAlpha=0;
else
_translucentAlpha=translucentAlpha;
toolBarBG.alpha=translucentAlpha;
}
#pragma mark - Managing the View Hierarchy
- (NSArray *) subviews {
// must exclude nonexistentSubview
if (initComplete) {
NSMutableArray *array = [NSMutableArray arrayWithArray:[super subviews]];
[array removeObject:nonexistentSubview];
return (NSArray *)array;
}
else {
return [super subviews];
}
}
- (void) sendSubviewToBack:(UIView *)view {
// must exclude nonexistentSubview
if (initComplete) {
[self insertSubview:view aboveSubview:toolbarContainerClipView];
return;
}
else
[super sendSubviewToBack:view];
}
- (void) insertSubview:(UIView *)view atIndex:(NSInteger)index {
// must exclude nonexistentSubview
if (initComplete) {
[super insertSubview:view atIndex:(index+1)];
}
else
[super insertSubview:view atIndex:index];
}
- (void) exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 {
// must exclude nonexistentSubview
if (initComplete)
[super exchangeSubviewAtIndex:(index1+1) withSubviewAtIndex:(index2+1)];
else
[super exchangeSubviewAtIndex:(index1) withSubviewAtIndex:(index2)];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end