我想绘制一个带有褪色黑色背景颜色的视图,并用一些圆圈遮盖。它应该看起来的方式是圆形部分应该是空心的,我应该能够看到下面的视图。
CALayer屏蔽并没有帮助我,或者我可能没有以适当的方式应用它们(请参阅下面的注释代码)。
下面是我的代码,附上的是我现在看到的截图。
预计:黑色褪色的视图中有空心圆圈。
任何建议
#pragma Initializer
- (id)initWithFrame:(CGRect)iFrame andHollowFrames:(NSArray *)iHollowFrames {
if ((self = [super initWithFrame:iFrame]) != nil) {
self.hollowFrames = [NSArray arrayWithArray:iHollowFrames];
//self.layer.backgroundColor = CGColorCreateCopyWithAlpha([UIColor blackColor].CGColor, 0.5);
CAShapeLayer *circle1 = [self circleAt:CGPointMake(20.0, 20.0) ForRadius:10.0 withColor:[UIColor blackColor].CGColor andDashPattern:NO];
CAShapeLayer *circle2 = [self circleAt:CGPointMake(120.0, 20.0) ForRadius:10.0 withColor:[UIColor blackColor].CGColor andDashPattern:NO];
CALayer *myLayer = [CAShapeLayer layer];
myLayer.frame = self.frame;
myLayer.backgroundColor = CGColorCreateCopyWithAlpha([UIColor blackColor].CGColor, 0.5);
[self.layer addSublayer:myLayer];
// [circle1 setMask:myLayer];
// [circle2 setMask:myLayer];
[self.layer addSublayer:circle1];
[self.layer addSublayer:circle2];
}
return self;
}
#pragma Drawing Methods
- (CAShapeLayer *)circleAt:(CGPoint)iPoint ForRadius:(CGFloat)iRadius withColor:(CGColorRef)iColor andDashPattern:(BOOL)isDashPattern {
CAShapeLayer *aSignalcircle = [CAShapeLayer layer];
aSignalcircle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(iPoint.x, iPoint.y, 2.0 * iRadius, 2.0 * iRadius) cornerRadius:iRadius].CGPath;
aSignalcircle.position = CGPointMake(0.0 - iRadius, 0.0 - iRadius);
aSignalcircle.fillColor = [UIColor clearColor].CGColor;
aSignalcircle.strokeColor = iColor;
aSignalcircle.lineWidth = kPSSignalStrokeWidth;
aSignalcircle.backgroundColor = [UIColor clearColor].CGColor;
if (isDashPattern) {
aSignalcircle.lineDashPattern = @[@1, @1];
}
return aSignalcircle;
}
答案 0 :(得分:2)
使用下面的代码:
- (void)addShadowView {
self.hollowFrames = @[[NSValue valueWithCGPoint:CGPointMake(20.0, 20.0)], [NSValue valueWithCGPoint:CGPointMake(120.0, 20.0)]];
int radius = 15.0;
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, point.CGPointValue.y, 2.0*radius, 2.0*radius) cornerRadius:radius];
[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];
}
答案 1 :(得分:0)
当我尝试使用我的代码执行某些操作时,我遇到了类似的问题,我通过将新图层设置为可视窗口的大小来修复此问题,该图层是我想要的颜色,然后填充颜色,然后使用背景成为一个colorClear,然后在那里使用面具(哦,然后发送回全屏幕)。您的问题是,在渲染图层时,背景颜色即使在剪切后也会显示,而不是通过该级别,因为它是视图堆栈的底部。