1

I need to implement blurred view with circle hole in it, that will work in iOS7+

I tried to use native UIToolbar, but get some strange behavior in iOS7, When I set toolbar translucent to YES, and apply the mask, its disappear. When I try to do the same things in iOS8 it works ok.

Code of init method of overlay view:

- (instancetype)initWithFrame:(CGRect)frame withTransparentCircleInCenet:(CGPoint)center radius:(CGFloat)radius withStrokeSize:(CGFloat)strokeSize andColor:(CGColorRef)strokeColor
{
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;
        //Use UIToolbar for Blur
        _nativeBlurView = [[UIToolbar alloc] initWithFrame:self.bounds];
        _nativeBlurView.barStyle = UIBarStyleBlack;
        _nativeBlurView.translucent = YES;
        [self.layer insertSublayer:_nativeBlurView.layer atIndex:0];

        //Create mask with hole
        CAShapeLayer *mask = [[CAShapeLayer alloc] init];
        mask.frame = _nativeBlurView.layer.bounds;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:mask.frame];
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];
        [maskPath appendPath:circlePath];
        [maskPath setUsesEvenOddFillRule:YES];
        mask.path = maskPath.CGPath;
        [mask setFillRule:kCAFillRuleEvenOdd];
        mask.fillColor = [[UIColor blackColor] CGColor];
        _nativeBlurView.layer.mask = mask;

        //draw circle over the hole
        UIBezierPath *strokeCirclePath  = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];
        CAShapeLayer *circle = [CAShapeLayer layer];
        circle.path = [strokeCirclePath CGPath];
        circle.strokeStart = 0;
        circle.strokeEnd = 1;
        circle.lineWidth = strokeSize;
        circle.strokeColor = strokeColor;
        circle.fillColor = [[UIColor clearColor] CGColor];
        circle.shadowOffset = CGSizeMake(0, strokeSize);
        circle.shadowOpacity = 0.5;
        circle.shadowColor = [[UIColor blackColor] CGColor];
        [self.layer insertSublayer:circle atIndex:1];

    }
    return self;
}

ScreenShots:

iOS7:
iOS7

iOS8:
iOS8

alaija
  • 31
  • 4
  • possible duplicate of [adding a layer mask to UIToolBar/UINavigationBar/UITabBar breaks translucency](http://stackoverflow.com/questions/20114461/adding-a-layer-mask-to-uitoolbar-uinavigationbar-uitabbar-breaks-translucency) – bummi May 10 '15 at 09:28

1 Answers1

0

I have rework non-native blur from UIERealTimeBlurView by Alex Usbergo and use it for blurred layer. This solution has very high CPU utilization, but it's ok for me.

alaija
  • 31
  • 4