How to make a transparent cut on UIVisualEffectView?

让人想犯罪 __ 提交于 2019-12-05 08:00:51

Add following global variables in your ViewController.h file-

CAShapeLayer *fillLayer;
UIVisualEffectView *overlayView;

Add following methods in your ViewController.m file-

-(void)addOverlay:(CGRect)rect{

    float x = rect.origin.x;
    float y = rect.origin.y;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, rect.size.width, rect.size.height) cornerRadius:5];

    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];

    [self removeOverlay];
    overlayView = [[UIVisualEffectView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height+64)];
    overlayView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:overlayView];

    fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;

    fillLayer.fillRule = kCAFillRuleEvenOdd;

    fillLayer.fillColor = [UIColor colorWithRed:78/255.0 green:103/255.0 blue:135/255.0 alpha:1.0].CGColor;
    fillLayer.opacity = 0.85;
    [[UIApplication sharedApplication].keyWindow.layer addSublayer:fillLayer];

}

-(void)removeOverlay{
    [overlayView removeFromSuperview];
    [fillLayer removeFromSuperlayer];
}

and call it as -

[self addOverlay:rect];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!