簡而言之,你必須通過所有的子視圖迭代,讓每一個修改當前的上下文路徑,然後設置路徑爲剪貼路徑。您可以從每個視圖請求路徑或將上下文傳遞給每個視圖。
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);
啊,我想,我幾乎擁有它。我假定每個子視圖的pathTranslatedForView:方法的主要任務之一是調整路徑的座標以說明子視圖相對於其超視圖的偏移量,但是最簡單的方法是什麼?在構建路徑之前設置轉換轉換? – hkatz 2010-04-20 01:28:17
它主要取決於您的實施細節。您是否在視圖中保留了實際的CGPathRef,或者是圓形和方形的位置?許多CGPath操作接受一個轉換,所以每個類都可以爲自己構建一個轉換。或者在調用每個子視圖之前,超視圖可以將上下文應用於上下文。 – drawnonward 2010-04-20 16:58:04