-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVFViewController+CWPopup.m
401 lines (353 loc) · 18.9 KB
/
AVFViewController+CWPopup.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#import "AVFViewController+CWPopup.h"
#import <objc/runtime.h>
#import <QuartzCore/QuartzCore.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
@import Accelerate;
#endif
#import <float.h>
@interface UIImage (ImageBlur)
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
@end
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
@implementation UIImage (ImageBlur)
// This method is taken from Apple's UIImageEffects category provided in WWDC 2013 sample code
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage
{
// Check pre-conditions.
if (self.size.width < 1 || self.size.height < 1) {
NSLog (@"*** error: invalid size: (%.2f x %.2f). Both dimensions must be >= 1: %@", self.size.width, self.size.height, self);
return nil;
}
if (!self.CGImage) {
NSLog (@"*** error: image must be backed by a CGImage: %@", self);
return nil;
}
if (maskImage && !maskImage.CGImage) {
NSLog (@"*** error: maskImage must be backed by a CGImage: %@", maskImage);
return nil;
}
CGRect imageRect = { CGPointZero, self.size };
UIImage *effectImage = self;
BOOL hasBlur = blurRadius > __FLT_EPSILON__;
BOOL hasSaturationChange = fabs(saturationDeltaFactor - 1.) > __FLT_EPSILON__;
if (hasBlur || hasSaturationChange) {
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef effectInContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(effectInContext, 1.0, -1.0);
CGContextTranslateCTM(effectInContext, 0, -self.size.height);
CGContextDrawImage(effectInContext, imageRect, self.CGImage);
vImage_Buffer effectInBuffer;
effectInBuffer.data = CGBitmapContextGetData(effectInContext);
effectInBuffer.width = CGBitmapContextGetWidth(effectInContext);
effectInBuffer.height = CGBitmapContextGetHeight(effectInContext);
effectInBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectInContext);
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef effectOutContext = UIGraphicsGetCurrentContext();
vImage_Buffer effectOutBuffer;
effectOutBuffer.data = CGBitmapContextGetData(effectOutContext);
effectOutBuffer.width = CGBitmapContextGetWidth(effectOutContext);
effectOutBuffer.height = CGBitmapContextGetHeight(effectOutContext);
effectOutBuffer.rowBytes = CGBitmapContextGetBytesPerRow(effectOutContext);
if (hasBlur) {
// A description of how to compute the box kernel width from the Gaussian
// radius (aka standard deviation) appears in the SVG spec:
// http://www.w3.org/TR/SVG/filters.html#feGaussianBlurElement
//
// For larger values of 's' (s >= 2.0), an approximation can be used: Three
// successive box-blurs build a piece-wise quadratic convolution kernel, which
// approximates the Gaussian kernel to within roughly 3%.
//
// let d = floor(s * 3*sqrt(2*pi)/4 + 0.5)
//
// ... if d is odd, use three box-blurs of size 'd', centered on the output pixel.
//
CGFloat inputRadius = blurRadius * [[UIScreen mainScreen] scale];
NSUInteger radius = floor(inputRadius * 3. * sqrt(2 * M_PI) / 4 + 0.5);
if (radius % 2 != 1) {
radius += 1; // force radius to be odd so that the three box-blur methodology works.
}
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (uint32_t) radius, (uint32_t) radius, 0, kvImageEdgeExtend);
vImageBoxConvolve_ARGB8888(&effectOutBuffer, &effectInBuffer, NULL, 0, 0, (uint32_t) radius, (uint32_t) radius, 0, kvImageEdgeExtend);
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (uint32_t) radius, (uint32_t) radius, 0, kvImageEdgeExtend);
}
BOOL effectImageBuffersAreSwapped = NO;
if (hasSaturationChange) {
CGFloat s = saturationDeltaFactor;
CGFloat floatingPointSaturationMatrix[] = {
0.0722 + 0.9278 * s, 0.0722 - 0.0722 * s, 0.0722 - 0.0722 * s, 0,
0.7152 - 0.7152 * s, 0.7152 + 0.2848 * s, 0.7152 - 0.7152 * s, 0,
0.2126 - 0.2126 * s, 0.2126 - 0.2126 * s, 0.2126 + 0.7873 * s, 0,
0, 0, 0, 1,
};
const int32_t divisor = 256;
NSUInteger matrixSize = sizeof(floatingPointSaturationMatrix)/sizeof(floatingPointSaturationMatrix[0]);
int16_t saturationMatrix[matrixSize];
for (NSUInteger i = 0; i < matrixSize; ++i) {
saturationMatrix[i] = (int16_t)roundf(floatingPointSaturationMatrix[i] * divisor);
}
if (hasBlur) {
vImageMatrixMultiply_ARGB8888(&effectOutBuffer, &effectInBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
effectImageBuffersAreSwapped = YES;
}
else {
vImageMatrixMultiply_ARGB8888(&effectInBuffer, &effectOutBuffer, saturationMatrix, divisor, NULL, NULL, kvImageNoFlags);
}
}
if (!effectImageBuffersAreSwapped)
effectImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (effectImageBuffersAreSwapped)
effectImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Set up output context.
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -self.size.height);
// Draw base image.
CGContextDrawImage(outputContext, imageRect, self.CGImage);
// Draw effect image.
if (hasBlur) {
CGContextSaveGState(outputContext);
if (maskImage) {
CGContextClipToMask(outputContext, imageRect, maskImage.CGImage);
}
CGContextDrawImage(outputContext, imageRect, effectImage.CGImage);
CGContextRestoreGState(outputContext);
}
// Add in color tint.
if (tintColor) {
CGContextSaveGState(outputContext);
CGContextSetFillColorWithColor(outputContext, tintColor.CGColor);
CGContextFillRect(outputContext, imageRect);
CGContextRestoreGState(outputContext);
}
// Output image is ready.
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
@end
#endif
#define ANIMATION_TIME 0.5f
#define STATUS_BAR_SIZE 22
NSString const *CWPopupKey = @"CWPopupkey";
NSString const *CWBlurViewKey = @"CWFadeViewKey";
NSString const *CWUseBlurForPopup = @"CWUseBlurForPopup";
NSString const *CWPopupViewOffset = @"CWPopupViewOffset";
@implementation UIViewController (CWPopup)
@dynamic popupViewController, useBlurForPopup, popupViewOffset;
#pragma mark - blur view methods
- (UIImage *)getScreenImage {
// frame without status bar
CGRect frame;
if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
} else {
frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
// begin image context
UIGraphicsBeginImageContext(frame.size);
// get current context
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// draw current view
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// clip context to frame
CGContextClipToRect(currentContext, frame);
// get resulting cropped screenshot
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
// end image context
UIGraphicsEndImageContext();
return screenshot;
}
- (UIImage *)getBlurredImage:(UIImage *)imageToBlur {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
return [imageToBlur applyBlurWithRadius:10.0f tintColor:[UIColor clearColor] saturationDeltaFactor:1.0 maskImage:nil];
}
return imageToBlur;
}
- (void)addBlurView {
UIImageView *blurView = [UIImageView new];
if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
blurView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
} else {
blurView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
blurView.alpha = 0.0f;
blurView.image = [self getBlurredImage:[self getScreenImage]];
[self.view addSubview:blurView];
[self.view bringSubviewToFront:self.popupViewController.view];
objc_setAssociatedObject(self, &CWBlurViewKey, blurView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - present/dismiss
- (void)presentPopupViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if (self.popupViewController == nil) {
// initial setup
self.popupViewController = viewControllerToPresent;
self.popupViewController.view.autoresizesSubviews = NO;
self.popupViewController.view.autoresizingMask = UIViewAutoresizingNone;
[self addChildViewController:viewControllerToPresent];
CGRect finalFrame = [self getPopupFrameForViewController:viewControllerToPresent];
// parallax setup if iOS7+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
interpolationHorizontal.minimumRelativeValue = @-10.0;
interpolationHorizontal.maximumRelativeValue = @10.0;
UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
interpolationHorizontal.minimumRelativeValue = @-10.0;
interpolationHorizontal.maximumRelativeValue = @10.0;
[self.popupViewController.view addMotionEffect:interpolationHorizontal];
[self.popupViewController.view addMotionEffect:interpolationVertical];
}
#endif
// shadow setup
viewControllerToPresent.view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
viewControllerToPresent.view.layer.shadowColor = [UIColor blackColor].CGColor;
viewControllerToPresent.view.layer.shadowRadius = 3.0f;
viewControllerToPresent.view.layer.shadowOpacity = 0.8f;
viewControllerToPresent.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:viewControllerToPresent.view.layer.bounds].CGPath;
// rounded corners
viewControllerToPresent.view.layer.cornerRadius = 5.0f;
// blurview
if (self.useBlurForPopup) {
[self addBlurView];
} else {
UIView *fadeView = [UIImageView new];
if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
fadeView.frame = [UIScreen mainScreen].bounds;
} else {
fadeView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
fadeView.backgroundColor = [UIColor blackColor];
fadeView.alpha = 0.0f;
[self.view addSubview:fadeView];
objc_setAssociatedObject(self, &CWBlurViewKey, fadeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
UIView *blurView = objc_getAssociatedObject(self, &CWBlurViewKey);
[viewControllerToPresent beginAppearanceTransition:YES animated:flag];
// setup
if (flag) { // animate
CGRect initialFrame = CGRectMake(finalFrame.origin.x, [UIScreen mainScreen].bounds.size.height + viewControllerToPresent.view.frame.size.height/2, finalFrame.size.width, finalFrame.size.height);
viewControllerToPresent.view.frame = initialFrame;
[self.view addSubview:viewControllerToPresent.view];
[UIView animateWithDuration:ANIMATION_TIME delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewControllerToPresent.view.frame = finalFrame;
blurView.alpha = self.useBlurForPopup ? 1.0f : 0.4f;
} completion:^(BOOL finished) {
[self.popupViewController didMoveToParentViewController:self];
[self.popupViewController endAppearanceTransition];
[completion invoke];
}];
} else { // don't animate
viewControllerToPresent.view.frame = finalFrame;
[self.view addSubview:viewControllerToPresent.view];
[self.popupViewController didMoveToParentViewController:self];
[self.popupViewController endAppearanceTransition];
[completion invoke];
}
// if screen orientation changed
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenOrientationChanged) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
}
- (void)dismissPopupViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
UIView *blurView = objc_getAssociatedObject(self, &CWBlurViewKey);
[self dismissViewControllerAnimated:YES completion:nil];
[self.popupViewController willMoveToParentViewController:nil];
[self.popupViewController beginAppearanceTransition:NO animated:flag];
// [self removeFromParentViewController];
if (flag) { // animate
CGRect initialFrame = self.popupViewController.view.frame;
[UIView animateWithDuration:ANIMATION_TIME delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.popupViewController.view.frame = CGRectMake(initialFrame.origin.x, [UIScreen mainScreen].bounds.size.height + initialFrame.size.height/2, initialFrame.size.width, initialFrame.size.height);
// uncomment the line below to have slight rotation during the dismissal
// self.popupViewController.view.transform = CGAffineTransformMakeRotation(M_PI/6);
blurView.alpha = 0.0f;
} completion:^(BOOL finished) {
[self.popupViewController removeFromParentViewController];
[self.popupViewController endAppearanceTransition];
[self.popupViewController.view removeFromSuperview];
[blurView removeFromSuperview];
self.popupViewController = nil;
[completion invoke];
}];
} else { // don't animate
[self.popupViewController removeFromParentViewController];
[self.popupViewController endAppearanceTransition];
[self.popupViewController.view removeFromSuperview];
[blurView removeFromSuperview];
self.popupViewController = nil;
blurView = nil;
[completion invoke];
}
// remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
#pragma mark - handling screen orientation change
- (CGRect)getPopupFrameForViewController:(UIViewController *)viewController {
CGRect frame = viewController.view.frame;
CGFloat x;
CGFloat y;
if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
x = ([UIScreen mainScreen].bounds.size.width - frame.size.width)/2;
y = ([UIScreen mainScreen].bounds.size.height - frame.size.height)/2;
} else {
x = ([UIScreen mainScreen].bounds.size.height - frame.size.width)/2;
y = ([UIScreen mainScreen].bounds.size.width - frame.size.height)/2;
}
return CGRectMake(x + viewController.popupViewOffset.x, y + viewController.popupViewOffset.y, frame.size.width, frame.size.height);
}
- (void)screenOrientationChanged {
// make blur view go away so that we can re-blur the original back
UIView *blurView = objc_getAssociatedObject(self, &CWBlurViewKey);
[UIView animateWithDuration:ANIMATION_TIME animations:^{
self.popupViewController.view.frame = [self getPopupFrameForViewController:self.popupViewController];
if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
blurView.frame = [UIScreen mainScreen].bounds;
} else {
blurView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
if (self.useBlurForPopup) {
[UIView animateWithDuration:1.0f animations:^{
// for delay
} completion:^(BOOL finished) {
[blurView removeFromSuperview];
// popup view alpha to 0 so its not in the blur image
self.popupViewController.view.alpha = 0.0f;
[self addBlurView];
self.popupViewController.view.alpha = 1.0f;
// display blurView again
UIView *blurView = objc_getAssociatedObject(self, &CWBlurViewKey);
blurView.alpha = 1.0f;
}];
}
}];
}
#pragma mark - popupViewController getter/setter
- (void)setPopupViewController:(UIViewController *)popupViewController {
objc_setAssociatedObject(self, &CWPopupKey, popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIViewController *)popupViewController {
return objc_getAssociatedObject(self, &CWPopupKey);
}
- (void)setUseBlurForPopup:(BOOL)useBlurForPopup {
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0 && useBlurForPopup) {
NSLog(@"ERROR: Blur unavailable prior to iOS 7");
objc_setAssociatedObject(self, &CWUseBlurForPopup, [NSNumber numberWithBool:NO], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} else {
objc_setAssociatedObject(self, &CWUseBlurForPopup, [NSNumber numberWithBool:useBlurForPopup], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
- (BOOL)useBlurForPopup {
NSNumber *result = objc_getAssociatedObject(self, &CWUseBlurForPopup);
return [result boolValue];
}
- (void)setPopupViewOffset:(CGPoint)popupViewOffset {
objc_setAssociatedObject(self, &CWPopupViewOffset, [NSValue valueWithCGPoint:popupViewOffset], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGPoint)popupViewOffset {
NSValue *offset = objc_getAssociatedObject(self, &CWPopupViewOffset);
return [offset CGPointValue];
}
@end