我正在尝试绘制一个透明的圆形 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/