ios - 用 CALayers mask 在褪色的黑色 View 中有空心圆圈

标签 ios cocoa-touch uiview calayer cashapelayer

我想绘制一个褪色的黑色背景颜色的 View ,并用一些圆圈遮盖。它的外观应该是圆形部分应该是空心的,我应该能够看到下面的 View 。

CALayer masking 没有帮助我,或者我没有以正确的方式应用它们(见下面的注释代码)。

下面是我的代码,附件是我现在看到的截图。

预期:黑色褪色 View ,其中有空心圆圈。

任何建议

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

enter image description here

最佳答案

使用下面的代码让它工作:

- (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/

相关文章:

objective-c - 有没有办法删除日期选择器的 "year"?

Swift - Spritekit - 按钮淡入/淡出

ios - NSPopover iOS版

iphone - 在 Cocoa Touch 环境中使用 AppKit 的等效 NSBox 是什么?

ios - 对齐表格单元格中的两个标签?

ios - 如何在自定义初始化程序中快速返回 nil?

ios - drawRect :中的EXC_BAD_ACCESS

Swift 3 - CGAffineTransform 不起作用

ios - 在 Swift 中向下转换 NSObject

ios - 尝试拒绝等待审核的二进制文件时出现 'you cannot reject the app version in the current state' 错误