0

スーパービューの選択した領域だけを透明にする方法はありますか? 背景色が黒 (アルファ 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 witch がデフォルト値のままにしておく必要があります。この 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 に答える