着色一个切口面具

时间:2014-08-27 14:22:28

标签: ios objective-c uiview cgcontext

我使用此功能作为功能的基础,使屏幕在圆形区域外变暗,并保持不变:

Mask a UIView with a cut-out circle

但我想做的是选择性地将圆圈着色为红色,但我的所有尝试都失败了。我哪里出错了?

[[self fillColor] set];
UIRectFill(rect);

CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect: self.circleFrame];
if (self.tintCircle)
{
    float red[4] = {1,0,0,1};
    CGContextSetFillColor(context, red);
    [path fill];
}
else
{
    [path fillWithBlendMode:kCGBlendModeDestinationOut alpha:1.0];
}

1 个答案:

答案 0 :(得分:0)

最后很容易。我不得不剪掉圆圈,然后再用红色画出来。

- (void)drawRect:(CGRect)rect
{
    [[self fillColor] set];
    UIRectFill(rect);

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect: self.circleFrame];
    [path fillWithBlendMode:kCGBlendModeDestinationOut alpha:1.0];

    if (self.drawRedCircle)
    {
        [[UIColor redColor] setFill];
        [path fillWithBlendMode:kCGBlendModeNormal alpha:0.5];
    }
}