在我正在编写的可视化应用程序中,我想使用通过路径创建的遮罩来塑造图形。该图形是一个水平矩形条,其中绘制了各种形状。在这个矩形条上,我想绘制一个椭圆作为遮罩,对矩形条进行整形,使其看起来像在椭圆内绘制。我该怎么做呢?
在这个简化的示例中,我正在尝试 - 并且未能通过使用各种混合模式在其顶部绘制一个椭圆来使背景蓝色矩形显示为蓝色椭圆。
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[[colorDictionary objectForKey:@"blueSolid"] setFill];
CGContextFillRect(context, self.bounds);
//CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
CGContextFillEllipseInRect(context, self.bounds);
}
谢谢,道格
更新:解决方案简而言之:剪切路径。呸!下面的代码片段创建了我所追求的效果,它是红色背景下的蓝色椭圆。请注意,蓝色椭圆是通过将蓝色矩形约束到椭圆的形状来创建的。这就是我需要的。凉爽的。
希望这对其他人有帮助。剪切路径非常强大。干杯。
CGContextRef context = UIGraphicsGetCurrentContext();
[[colorDictionary objectForKey:@"redSolid"] setFill];
CGContextFillRect(context, self.bounds);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, CGRectInset(self.bounds, 32, 32));
CGContextClip(context);
[[colorDictionary objectForKey:@"blueSolid"] setFill];
CGContextFillRect(context, self.bounds);