我想画一个带有褪色的黑色背景色并带有一些圆圈的视图。它的外观应该是圆形部分是空心的,我应该能够看到下面的视图。

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;
}

最佳答案

在下面的代码中工作了:

- (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];
}

关于ios - 使用CALayers mask 以在褪色的黑色 View 中具有空心圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18924237/

10-11 14:54