Skip to content

Instantly share code, notes, and snippets.

@zhangxigithub
Created September 1, 2014 15:24
Show Gist options
  • Save zhangxigithub/3326003d3f9d24b9731b to your computer and use it in GitHub Desktop.
Save zhangxigithub/3326003d3f9d24b9731b to your computer and use it in GitHub Desktop.
UIView+ZXPop.h
//
// UIView+ZXPop.h
// sdhgasdgf
//
// Created by 张玺 on 14-9-1.
// Copyright (c) 2014年 zhangxi.me. All rights reserved.
//
#define kZXPopBlurImageTag -5531
#define kZXPopBlurViewTag -5532
#define kZXPopViewTag -5533
#define kZXPopDuration 0.3
#define kZXPopAlpha 0.3
#import <UIKit/UIKit.h>
@interface UIView (ZXPop)
//pop
-(void)zxPopInView:(UIView *)view;
-(void)zxPopInView:(UIView *)view animated:(BOOL)animated;
//hide
-(void)zxPopHide;
-(void)zxPopHideWithAnimated:(BOOL)animated;
//hide after xx second
-(void)zxPopHideAfter:(NSTimeInterval)second;
-(void)zxPopHideAfter:(NSTimeInterval)second animated:(BOOL)animated;
//hide all
-(void)hideAllPopedView;
-(void)hideAllPopedViewWithAnimated:(BOOL)animated;
//hide all after xx second
-(void)hideAllPopedViewAfter:(NSTimeInterval)second;
-(void)hideAllPopedViewWithAnimated:(BOOL)animated after:(NSTimeInterval)second;
-(void)blur;
-(void)unBlur;
@end
//
// UIView+ZXPop.m
// sdhgasdgf
//
// Created by 张玺 on 14-9-1.
// Copyright (c) 2014年 zhangxi.me. All rights reserved.
//
#import "UIView+ZXPop.h"
@implementation UIView (ZXPop)
-(void)zxPopInView:(UIView *)view
{
[self zxPopInView:view animated:YES];
}
-(void)zxPopInView:(UIView *)view animated:(BOOL)animated
{
[view blur];
[view addSubview:self];
self.center = CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2);
self.tag = kZXPopViewTag;
if(animated)
{
self.alpha = kZXPopAlpha;
[UIView animateWithDuration:kZXPopDuration
animations:^{
self.center = CGPointMake(view.bounds.size.width/2, view.bounds.size.height/2);
self.alpha = 1;
}];
}
}
-(void)zxPopHide
{
[self zxPopHideWithAnimated:YES];
}
-(void)zxPopHideWithoutAnimate
{
[self zxPopHideWithAnimated:NO];
}
-(void)zxPopHideWithAnimated:(BOOL)animated
{
[self.superview unBlur];
if(animated)
{
[UIView animateWithDuration:kZXPopDuration
animations:^{
self.alpha = kZXPopAlpha;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}else
{
[self removeFromSuperview];
}
}
-(void)zxPopHideAfter:(NSTimeInterval)second
{
[self zxPopHideAfter:second animated:YES];
}
-(void)zxPopHideAfter:(NSTimeInterval)second animated:(BOOL)animated
{
if(animated)
{
[self performSelector:@selector(zxPopHide)
withObject:nil
afterDelay:second];
}else
{
[self performSelector:@selector(zxPopHideWithoutAnimate)
withObject:nil
afterDelay:second];
}
}
-(void)hideAllPopedView
{
[self hideAllPopedViewWithAnimated:YES];
}
-(void)hideAllPopedViewWithoutAnimate
{
[self hideAllPopedViewWithAnimated:NO];
}
-(void)hideAllPopedViewWithAnimated:(BOOL)animated
{
UIView *view = [self viewWithTag:kZXPopViewTag];
[view.superview unBlur];
[view zxPopHideWithAnimated:animated];
}
-(void)hideAllPopedViewAfter:(NSTimeInterval)second
{
[self performSelector:@selector(hideAllPopedView)
withObject:nil
afterDelay:second];
}
-(void)hideAllPopedViewWithAnimated:(BOOL)animated after:(NSTimeInterval)second
{
if(animated)
{
[self performSelector:@selector(hideAllPopedView)
withObject:nil
afterDelay:second];
}else
{
[self performSelector:@selector(hideAllPopedViewWithoutAnimate)
withObject:nil
afterDelay:second];
}
}
- (void)blur{
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:15] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
CGImageRef cgImage = [context createCGImage:resultImage fromRect:self.bounds];
UIImage *blurredImage = [UIImage imageWithCGImage:cgImage];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.tag = kZXPopBlurImageTag;
imageView.image = blurredImage;
UIView *overlay = [[UIView alloc] initWithFrame:self.bounds];
overlay.tag = kZXPopBlurViewTag;
overlay.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
[self addSubview:imageView];
[self addSubview:overlay];
}
-(void)unBlur{
[[self viewWithTag:kZXPopBlurImageTag] removeFromSuperview];
[[self viewWithTag:kZXPopBlurViewTag] removeFromSuperview];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment