Created
September 21, 2016 15:33
-
-
Save emrahgunduz/ee4729c93d13715299ede2c267ea7941 to your computer and use it in GitHub Desktop.
Working UIBlurEffect and UIVisualEffectView masking example on iOS10 using ObjectiveC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "self" in here is an UIView that contains some images inside. | |
{ | |
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; | |
UIVisualEffectView *blurredEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; | |
CGRect frame = self.frame; | |
frame.origin = CGPointMake (0, 0); | |
blurredEffectView.frame = frame; | |
[self addSubview:blurredEffectView]; | |
UIView *maskView = [[UIView alloc] initWithFrame:frame]; | |
maskView.backgroundColor = [UIColor blackColor]; | |
__weak UIView *weak = self; | |
maskView.layer.mask = ({ // This mask draws a rectangle and crops a circle inside it. | |
__strong UIView *strong = weak; | |
CGRect roundedRect = CGRectMake ( | |
0, | |
0, | |
strong.frame.size.width * 0.8f, | |
strong.frame.size.width * 0.8f | |
); | |
roundedRect.origin.x = strong.frame.size.width / 2 - roundedRect.size.width / 2; | |
roundedRect.origin.y = strong.frame.size.height / 2 - roundedRect.size.height / 2; | |
CGFloat cornerRadius = roundedRect.size.height / 2.0f; | |
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds]; | |
UIBezierPath *croppedPath = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:cornerRadius]; | |
[path appendPath:croppedPath]; | |
[path setUsesEvenOddFillRule:YES]; | |
CAShapeLayer *mask = [CAShapeLayer layer]; | |
mask.path = path.CGPath; | |
mask.fillRule = kCAFillRuleEvenOdd; | |
mask; | |
}); | |
blurredEffectView.maskView = maskView; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works only if i present a view controller which has this, but doesnot work if i embed the viewcontroller in a navigation controller. Why? How can i prevent it