2013-10-08 60 views
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]; 
} 
+0

提供代碼如何在視圖上放置空心圓。 – Unheilig

+0

添加了上面的代碼。 – Abhinav

回答

0

這是一個UIView子類的解決方案。爲了這個工作,你必須保留superviews backgroundColor屬性爲無巫婆是默認值。如果此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); 
}