forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXHRealTimeBlur.m
More file actions
304 lines (242 loc) · 9.07 KB
/
Copy pathXHRealTimeBlur.m
File metadata and controls
304 lines (242 loc) · 9.07 KB
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
//
// XHRealTimeBlur.m
// XHRealTimeBlurExample
//
// Created by 曾 宪华 on 14-9-7.
// Copyright (c) 2014年 曾宪华 QQ群: (142557668) QQ:543413507 Gmail:xhzengAIB@gmail.com. All rights reserved.
//
#import "XHRealTimeBlur.h"
@interface XHGradientView : UIView
@end
@implementation XHGradientView
+ (Class)layerClass {
return [CAGradientLayer class];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
CAGradientLayer *gradientLayer = (CAGradientLayer *)self.layer;
gradientLayer.colors = @[
(id)[[UIColor colorWithWhite:0 alpha:1] CGColor],
(id)[[UIColor colorWithWhite:0 alpha:0.5] CGColor],
];
}
return self;
}
@end
@interface XHRealTimeBlur ()
@property (nonatomic, strong) XHGradientView *gradientBackgroundView;
@property (nonatomic, strong) UIToolbar *blurBackgroundView, *blurWhiteBackgroundView;
@property (nonatomic, strong) UIView *blackTranslucentBackgroundView;
@property (nonatomic, strong) UIView *whiteBackgroundView;
@end
@implementation XHRealTimeBlur
- (void)showBlurViewAtView:(UIView *)currentView {
[self showAnimationAtContainerView:currentView];
}
- (void)showBlurViewAtViewController:(UIViewController *)currentViewContrller {
[self showAnimationAtContainerView:currentViewContrller.view];
}
- (void)disMiss {
[self hiddenAnimation];
}
#pragma mark - Private
- (void)showAnimationAtContainerView:(UIView *)containerView {
if (self.showed) {
[self disMiss];
return;
} else {
if (self.willShowBlurViewcomplted) {
self.willShowBlurViewcomplted();
}
}
self.alpha = 0.0;
[containerView insertSubview:self atIndex:0];
[UIView animateWithDuration:self.showDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.alpha = 1.0;
} completion:^(BOOL finished) {
self.showed = YES;
if (self.didShowBlurViewcompleted) {
self.didShowBlurViewcompleted(finished);
}
}];
}
- (void)hiddenAnimation {
[self hiddenAnimationCompletion:^(BOOL finished) {
}];
}
- (void)hiddenAnimationCompletion:(void (^)(BOOL finished))completion {
if (self.willDismissBlurViewCompleted) {
self.willDismissBlurViewCompleted();
}
[UIView animateWithDuration:self.disMissDuration delay:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
if (completion) {
completion(finished);
}
if (self.didDismissBlurViewCompleted) {
self.didDismissBlurViewCompleted(finished);
}
self.showed = NO;
[self removeFromSuperview];
}];
}
- (void)handleTapGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer {
[self hiddenAnimationCompletion:^(BOOL finished) {
}];
}
#pragma mark - Propertys
- (void)setHasTapGestureEnable:(BOOL)hasTapGestureEnable {
_hasTapGestureEnable = hasTapGestureEnable;
[self setupTapGesture];
}
- (XHGradientView *)gradientBackgroundView {
if (!_gradientBackgroundView) {
_gradientBackgroundView = [[XHGradientView alloc] initWithFrame:self.bounds];
}
return _gradientBackgroundView;
}
- (UIToolbar *)blurBackgroundView {
if (!_blurBackgroundView) {
_blurBackgroundView = [[UIToolbar alloc] initWithFrame:self.bounds];
[_blurBackgroundView setBarStyle:UIBarStyleBlackTranslucent];
}
return _blurBackgroundView;
}
- (UIToolbar *)blurWhiteBackgroundView {
if (!_blurWhiteBackgroundView) {
_blurWhiteBackgroundView = [[UIToolbar alloc] initWithFrame:self.bounds];
[_blurWhiteBackgroundView setBarStyle:UIBarStyleDefault];
}
return _blurWhiteBackgroundView;
}
- (UIView *)blackTranslucentBackgroundView {
if (!_blackTranslucentBackgroundView) {
_blackTranslucentBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
_blackTranslucentBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
}
return _blackTranslucentBackgroundView;
}
- (UIView *)whiteBackgroundView {
if (!_whiteBackgroundView) {
_whiteBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
_whiteBackgroundView.backgroundColor = [UIColor clearColor];
_whiteBackgroundView.tintColor = [UIColor colorWithWhite:0.2 alpha:1.0];
}
return _whiteBackgroundView;
}
- (UIView *)backgroundView {
switch (self.blurStyle) {
case XHBlurStyleBlackGradient:
return self.gradientBackgroundView;
break;
case XHBlurStyleTranslucent:
return self.blurBackgroundView;
case XHBlurStyleTranslucentWhite:
return self.blurWhiteBackgroundView;
case XHBlurStyleBlackTranslucent:
return self.blackTranslucentBackgroundView;
break;
case XHBlurStyleWhite:
return self.whiteBackgroundView;
break;
default:
break;
}
}
#pragma mark - Life Cycle
- (void)setup {
self.showDuration = self.disMissDuration = 0.3;
self.blurStyle = XHBlurStyleTranslucent;
self.backgroundColor = [UIColor clearColor];
_hasTapGestureEnable = NO;
}
- (void)setupTapGesture {
if (self.hasTapGestureEnable) {
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
[self addGestureRecognizer:tapGestureRecognizer];
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview) {
UIView *backgroundView = [self backgroundView];
backgroundView.userInteractionEnabled = NO;
[self addSubview:backgroundView];
}
}
@end
#pragma mark - UIView XHRealTimeBlur分类的实现
@implementation UIView (XHRealTimeBlur)
#pragma mark - Show Block
- (WillShowBlurViewBlcok)willShowBlurViewcomplted {
return objc_getAssociatedObject(self, &XHRealTimeWillShowBlurViewBlcokBlcokKey);
}
- (void)setWillShowBlurViewcomplted:(WillShowBlurViewBlcok)willShowBlurViewcomplted {
objc_setAssociatedObject(self, &XHRealTimeWillShowBlurViewBlcokBlcokKey, willShowBlurViewcomplted, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (DidShowBlurViewBlcok)didShowBlurViewcompleted {
return objc_getAssociatedObject(self, &XHRealTimeDidShowBlurViewBlcokBlcokKey);
}
- (void)setDidShowBlurViewcompleted:(DidShowBlurViewBlcok)didShowBlurViewcompleted {
objc_setAssociatedObject(self, &XHRealTimeDidShowBlurViewBlcokBlcokKey, didShowBlurViewcompleted, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#pragma mark - dismiss block
- (WillDismissBlurViewBlcok)willDismissBlurViewCompleted {
return objc_getAssociatedObject(self, &XHRealTimeWillDismissBlurViewBlcokKey);
}
- (void)setWillDismissBlurViewCompleted:(WillDismissBlurViewBlcok)willDismissBlurViewCompleted {
objc_setAssociatedObject(self, &XHRealTimeWillDismissBlurViewBlcokKey, willDismissBlurViewCompleted, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (DidDismissBlurViewBlcok)didDismissBlurViewCompleted {
return objc_getAssociatedObject(self, &XHRealTimeDidDismissBlurViewBlcokKey);
}
- (void)setDidDismissBlurViewCompleted:(DidDismissBlurViewBlcok)didDismissBlurViewCompleted {
objc_setAssociatedObject(self, &XHRealTimeDidDismissBlurViewBlcokKey, didDismissBlurViewCompleted, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#pragma mark - RealTimeBlur HUD
- (XHRealTimeBlur *)realTimeBlur {
return objc_getAssociatedObject(self, &XHRealTimeBlurKey);
}
- (void)setRealTimeBlur:(XHRealTimeBlur *)realTimeBlur {
objc_setAssociatedObject(self, &XHRealTimeBlurKey, realTimeBlur, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - 分类 公开方法
- (void)showRealTimeBlurWithBlurStyle:(XHBlurStyle)blurStyle {
[self showRealTimeBlurWithBlurStyle:blurStyle hasTapGestureEnable:NO];
}
- (void)showRealTimeBlurWithBlurStyle:(XHBlurStyle)blurStyle hasTapGestureEnable:(BOOL)hasTapGestureEnable {
XHRealTimeBlur *realTimeBlur = [self realTimeBlur];
if (!realTimeBlur) {
realTimeBlur = [[XHRealTimeBlur alloc] initWithFrame:self.bounds];
realTimeBlur.blurStyle = blurStyle;
[self setRealTimeBlur:realTimeBlur];
}
realTimeBlur.hasTapGestureEnable = hasTapGestureEnable;
realTimeBlur.willShowBlurViewcomplted = self.willShowBlurViewcomplted;
realTimeBlur.didShowBlurViewcompleted = self.didShowBlurViewcompleted;
realTimeBlur.willDismissBlurViewCompleted = self.willDismissBlurViewCompleted;
realTimeBlur.didDismissBlurViewCompleted = self.didDismissBlurViewCompleted;
[realTimeBlur showBlurViewAtView:self];
}
- (void)disMissRealTimeBlur {
[[self realTimeBlur] disMiss];
}
@end