在iOS7的translusent UIToolbar上挖洞

时间:2014-09-08 12:27:20

标签: ios7 uiview ios8 uitoolbar blur

我需要在其中实现带有圆孔的模糊视图,这将适用于iOS7 +

我尝试使用原生UIToolbar,但在iOS7中获得了一些奇怪的行为,当我将工具栏半透明设置为YES并应用蒙版时,它会消失。当我尝试在iOS8中执行相同的操作时,它可以正常工作。

叠加视图的init方法代码:

- (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;
}

截图:

iOS7:
iOS7

iOS8上:
iOS8

1 个答案:

答案 0 :(得分:0)

我已经通过Alex Usbergo的UIERealTimeBlurView修改了非原生模糊,并将其用于模糊图层。该解决方案具有非常高的CPU利用率,但对我来说还可以。