2017-02-25 77 views
0

如何在單個視圖中繪製多個遮罩層,這裏是我使用的代碼,但只有一個遮罩層正在工作其餘路徑刪除。 一次傳遞一個路徑,並在循環中調用它只是保持最後一個路徑掩碼刪除其餘的。多個bezierpaths掩蔽視圖的路徑

- (void)setClippingPath:(UIBezierPath *)clippingPath andView:(UIView *)view; 
{ 
    if (![[view layer] mask]) 
     [[view layer] setMask:[CAShapeLayer layer]]; 

    [(CAShapeLayer*) [[view layer] mask] setPath:[clippingPath CGPath]]; 
} 

回答

1

我在一些研究後解決了你的問題。我確實如下。首先我宣佈一個實例變量UIBezierPath *resultingPath。這個變量將被用於你的方法來追加傳入的路徑。

- (void)setClippingPath:(UIBezierPath *)clippingPath andView:(UIView *)view; 
    { 
     if (![[view layer] mask]) { 
     [[view layer] setMask:[CAShapeLayer layer]]; 
    } 

    if (resultingPath) { 
     [resultingPath appendPath:clippingPath]; 
     [(CAShapeLayer*) [[view layer] mask] setPath:[resultingPath CGPath]]; 
    } 
    } 

我測試了這個,它工作!

如果你喜歡TOR重置人,經過一番研究,我發現這個解決方案:

- (IBAction)resetMask:(id)sender { 
     UIBezierPath *hole = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     [self setClippingPath:hole andView:[self view]]; 
     resultingPath = [UIBezierPath bezierPath]; 
    } 

希望它能幫助。