0

有什么办法可以只使超级视图的选定区域透明?我有一个黑色(alpha 0.5)背景颜色的 UIView。我在 UIView 上放了一些空心圆圈,并希望下面的 UIView 是透明的,这样我就可以看穿它。任何想法

- (id)initWithFrame:(CGRect)iFrame andHollowFrames:(NSArray *)iHollowFrames withRadius:(NSNumber *)iRadius {
    if ((self = [super initWithFrame:iFrame]) != nil) {
        self.hollowFrames =  [[NSSet setWithArray:iHollowFrames] allObjects];
        self.hollowCircleRadius = iRadius;
        [self addShadowView];
    }

    return self;
}


- (void)addShadowView {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) cornerRadius:0];

    for (NSValue *point in self.hollowFrames) {
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(point.CGPointValue.x - self.hollowCircleRadius.floatValue, point.CGPointValue.y - self.hollowCircleRadius.floatValue, 2.0 * self.hollowCircleRadius.floatValue, 2.0 * self.hollowCircleRadius.floatValue) cornerRadius:self.hollowCircleRadius.floatValue];

        [path appendPath:circlePath];
    }

    [path setUsesEvenOddFillRule:YES];

    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor blackColor].CGColor;
    fillLayer.opacity = 0.5;
    [self.layer addSublayer:fillLayer];
}
4

1 回答 1

0

这是一个 UIView 子类解决方案。为了使它起作用,您必须将 superviews backgroundColor 属性保留为 nil 女巫是默认值。如果此 UIView 子类在情节提要中,请确保在属性检查器中将背景颜色设置为“清除颜色”。

- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIColor *blackBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    CGContextSetFillColorWithColor(ctx, blackBackgroundColor.CGColor);
    CGContextFillRect(ctx, rect);
    CGRect transparentPart = CGRectMake(10, 10, 120, 80);
    CGContextClearRect(ctx, transparentPart);
}
于 2013-11-05T17:44:48.867 回答