ios - 在 UIView 中绘制圆形透明 View

标签 ios objective-c uiview transparency

我正在尝试绘制一个透明的圆形 UIView。基于answer从这个问题中,我决定创建一个名为 PartialTransparentView 的 UIView 子类来创建动画透明圆形 View 。

我在该子类的实现中的 drawRect 方法中遇到问题,无法使绘制透明 View 实际显示为圆形而不是默认的矩形。基于以下代码和此answer的建议,我应该先设置上下文,然后再填充颜色。但是,即使在尝试了其他几种代码排列之后,它仍然无法正常工作。任何建议都会非常有帮助!

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();
    [backgroundColor setFill];
    UIRectFill(rect);

    for (NSValue *holeRectValue in rectsArray) {

        CGRect holeRect = [holeRectValue CGRectValue];
        CGContextAddEllipseInRect(context, holeRect);
        CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );

        [[UIColor clearColor] setFill];
        UIRectFill(holeRectIntersection);
    }

}

最佳答案

你需要在删除你的圈子之前改变混合模式:

CGContextSetBlendMode(context, kCGBlendModeClear)

关于ios - 在 UIView 中绘制圆形透明 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27179481/

相关文章:

objective-c - "Game Center account services are currently unavailable"在模拟器上

ios - 在 UIWebView 中禁用用户选择

ios - JSON 写入中的顶级类型无效

ios - 从后台恢复后应用程序屏幕变成全黑

ios - 如何识别 UIView subview 上的触摸

ios - UIView.transition 显示深色背景的父 View (Swift)

iOS RestKit block

ios - 在 Swift 中,Array [String] 切片返回类型似乎不是 [String]

iphone - 在 EKEventStore 中检索具有特定标题的 EKEvent

iphone - 如何使 'touchesBegan' 方法适用于特定 View ?