iPhone 石英。如何使用路径而不是位图创建遮罩?
在我正在编写的可视化应用程序中,我想使用通过路径创建的遮罩来塑造图形。该图形是一个水平矩形条,内部绘制有各种形状。在这个矩形条的顶部,我想绘制一个椭圆作为遮罩,塑造矩形条的形状,使其看起来像是在椭圆内绘制的。我该怎么做?
在这个简化的示例中,我尝试通过使用各种混合模式在其顶部绘制椭圆来使背景蓝色矩形显示为蓝色椭圆形,但失败了。
- (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);
In the visualization app I'm writing I want to shape a graphic using a matte created with a path. The graphic is a horizontal rectangular strip with various shapes drawn within. Atop this rectangular strip I want to draw an ellipse to act as a matte that shapes the rectangular strip to make it appear to drawn within the ellipse. How do I do this?
In this simplified example, I am trying - and failing to make the background blue rectangle to appear as a blue ellipes by drawing an ellipse atop it using various blend modes.
- (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);
}
Thanks,
Doug
UPDATE: SOLUTION
In brief: clipping paths. Dooh! The following code snippet creates the effect I am after which is a blue ellipse against a red background. Note, the blue ellipse is created by constraining a blue rectangle to the shape of the ellipse. This is what I need. Cool.
Hope this helps someone else. Clipping paths are super powerful. Cheers.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之:剪切路径。哎哟!下面的代码片段创建了我想要的效果,它是红色背景下的蓝色椭圆。请注意,蓝色椭圆是通过将蓝色矩形约束为椭圆的形状而创建的。这就是我需要的。凉爽的。
希望这对其他人有帮助。剪切路径非常强大。干杯。
In brief: clipping paths. Dooh! The following code snippet creates the effect I am after which is a blue ellipse against a red background. Note, the blue ellipse is created by constraining a blue rectangle to the shape of the ellipse. This is what I need. Cool.
Hope this helps someone else. Clipping paths are super powerful. Cheers.