从 UIView 子视图中的路径对象构建剪辑区域

发布于 2024-08-29 10:43:00 字数 257 浏览 6 评论 0原文

我试图在 UIView 中生成一个剪切区域,该区域是从其子视图中的路径对象生成的。例如,我可能有一个包含正方形的子视图,另一个包含圆形的子视图。我希望能够在父超级视图中生成一个剪辑,它是这两个形状的联合。有人可以解释如何做到这一点吗?到目前为止我能弄清楚的是:

1 - 超级视图的drawRect:方法在其子视图的drawRect之前被调用,并且
2 - 在所有三个实例中可访问的 ContextRef 是相同的。

除此之外我很困惑。 谢谢,
霍华德

I'm trying to produce a clipping area in a UIView that's generated from path objects in its subviews. For example, I might have one subview containing a square and another containing a circle. I want to be able to produce a clip in the parent superview that's the union of both these shapes. Can someone explain how to do this? About all I've been able to figure out so far is that:

1 - the superview's drawRect: method is called before its subviews' drawRects are, and
2 - the ContextRef that's accessible in all three instances is the same.

Other than that I'm stumped.
Thanks,
Howard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-09-05 10:43:00

简而言之,您必须迭代所有子视图并让每个子视图修改当前上下文路径,然后将该路径设置为剪切路径。您可以请求每个视图的路径或将上下文传递给每个视图。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState( context );
CGContextBeginPath( context );

for ( view in subviews ) {
  if ( [view respondsToSelector:@selector(pathTranslatedForView:)] ) {
    CGContextAddPath( context , [(MyCustomViewPath *)view pathTranslatedForView:self] );
  }
}

CGContextClip( context );
// do stuff with clipping path
CGContextRestoreGState( context );

编辑:

当调用子视图时,您可能需要调整上下文,以便 0,0 是被调用视图的原点,而不是超级视图:

CGRect frame = [view frame];
CGContextTranslateCTM( context , -frame.origin.x , -frame.origin.y );
CGContextAddPath( context , [(MyCustomViewPath *)view path]; 
CGContextTranslateCTM( context , frame.origin.x , frame.origin.y );

In a nutshell, you have to iterate through all the subviews and let each one modify the current context path, then set that path as the clipping path. You could ask for a path from each view or pass the context to each view.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState( context );
CGContextBeginPath( context );

for ( view in subviews ) {
  if ( [view respondsToSelector:@selector(pathTranslatedForView:)] ) {
    CGContextAddPath( context , [(MyCustomViewPath *)view pathTranslatedForView:self] );
  }
}

CGContextClip( context );
// do stuff with clipping path
CGContextRestoreGState( context );

Edit:

When calling the subview, you may want to adjust the context so that 0,0 is the origin of the called view, not the superview:

CGRect frame = [view frame];
CGContextTranslateCTM( context , -frame.origin.x , -frame.origin.y );
CGContextAddPath( context , [(MyCustomViewPath *)view path]; 
CGContextTranslateCTM( context , frame.origin.x , frame.origin.y );
空城之時有危險 2024-09-05 10:43:00

也许尝试将背景颜色设置为.clear:UIView - 矩形将消失仅租赁路径形状

Maybe try to set the background Color To .clear: the UIView - Rectangle will disappear Leasing just the path shape

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文