Building a clip area in a UIView from path objects in its subviews
I'm trying to pr开发者_开发问答oduce 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,
HowardIn 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 );
Maybe try to set the background Color To .clear: the UIView - Rectangle will disappear Leasing just the path shape
精彩评论