Skip to content

Instantly share code, notes, and snippets.

@jordandobson
Created June 11, 2015 00:11
Show Gist options
  • Save jordandobson/e6b39f9454452a615960 to your computer and use it in GitHub Desktop.
Save jordandobson/e6b39f9454452a615960 to your computer and use it in GitHub Desktop.
Apply blur to a view
- (UIView *)applyBlurToView:(UIView *)view withEffectStyle:(UIBlurEffectStyle)style andConstraints:(BOOL)addConstraints {
//only apply the blur if the user hasn't disabled transparency effects
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:style];
UIVisualEffectView * blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = view.bounds;
[view addSubview:blurEffectView];
blurEffectView.layer.opacity = 0;
if (addConstraints) {
//add auto layout constraints so that the blur fills the screen upon rotating device
[blurEffectView setTranslatesAutoresizingMaskIntoConstraints:NO];
[view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0]];
}
[UIView animateWithDuration: .5 delay: 0 options: UIViewAnimationOptionAllowUserInteraction animations: ^{ blurEffectView.layer.opacity = 1; } completion: nil];
} else {
view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
}
return view;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment