如何在UIVisualEffectView上做一个透明的裁剪?

6
在我的应用程序中,我通过子类化简单的UIView来创建一个透明的UIView。然而,如果我尝试使用UIVisualEffectView做同样的事情,我无法实现它。
以下是我使用普通UIView能够做到的:

enter image description here

当我使用UIVisualEffectView来代替绿色的UIView时,即使透明的UIView作为subview添加到UIVisualEffectView中,我也看不到透明的UIView

enter image description here

代码:

- (void)drawRect:(CGRect)rect { //this is same for the UIVIew and for the UIVisualEffectView
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    // Clear any existing drawing on this view
    // Remove this if the hole never changes on redraws of the UIView
    CGContextClearRect(context, self.bounds);

    // Create a path around the entire view
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:self.bounds];

    // Your transparent window. This is for reference, but set this either as a property of the class or some other way
    CGRect transparentFrame;
    // Add the transparent window
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:transparentFrame cornerRadius:5.0f];
    [clipPath appendPath:path];

    // NOTE: If you want to add more holes, simply create another UIBezierPath and call [clipPath appendPath:anotherPath];

    // This sets the algorithm used to determine what gets filled and what doesn't
    clipPath.usesEvenOddFillRule = YES;
    // Add the clipping to the graphics context
    [clipPath addClip];

    // set your color
    UIColor *tintColor = [UIColor greenColor]; 

    // (optional) set transparency alpha
    CGContextSetAlpha(context, 0.7f);
    // tell the color to be a fill color
    [tintColor setFill];
    // fill the path
    [clipPath fill];
}

问题: 为什么使用 UIVisualEffectView 时没有起作用?


嗨Teja,您能够实现此帖子中提到的功能吗?如果可以,您能否帮助我或建议一些代码调整,以便实现以下帖子中给出的代码。http://stackoverflow.com/questions/39165751/circle-masking-for-image-cropping-in-ios?noredirect=1#comment65676404_39165751 - sree_iphonedev
@sree_iphonedev 我一到电脑前就会做! - Teja Nandamuri
你最终解决了吗? - Roi Mulia
不在可视化效果视图下。 - Teja Nandamuri
1个回答

0
在你的 ViewController.h 文件中添加以下全局变量-
CAShapeLayer *fillLayer;
UIVisualEffectView *overlayView;

在您的ViewController.m文件中添加以下方法-
-(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];
}

并将其称为 -

[self addOverlay:rect];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接