2012-08-15 41 views
2

我試圖建立一個插圖陰影一個UIButton,由於其他SO帖子,我已經成功地做到這一點:如何在Objective-C中設置CAShapeLayer透明?

 UIBezierPath *buttonPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.layer.cornerRadius]; 

     CAShapeLayer* shadowLayer = [CAShapeLayer layer]; 
     //shadowLayer.cornerRadius = 8.0f; 
     [shadowLayer setOpacity:1]; 
     //[shadowLayer setBackgroundColor:UIColor.redColor.CGColor]; //not working 

     // Standard shadow stuff 
     [shadowLayer setShadowOpacity:1.0f]; 
     [shadowLayer setShadowColor:[[UIColor colorWithWhite:1 alpha:1] CGColor]]; 
     [shadowLayer setShadowOffset:CGSizeMake(2.0f, 2.0f)]; 
     [shadowLayer setShadowRadius:5]; 
     // Causes the inner region in this example to NOT be filled. 
     [shadowLayer setFillRule:kCAFillRuleEvenOdd]; 

     // Create the larger rectangle path. 
     CGMutablePathRef path = CGPathCreateMutable(); 
     CGPathAddRect(path, NULL, CGRectInset(self.bounds, -42.0f, -42.0f)); 
     // Add the inner path so it's subtracted from the outer path. 
     CGPathAddPath(path, NULL, buttonPath.CGPath); 
     CGPathCloseSubpath(path); 

     [shadowLayer setPath:path]; 
     CGPathRelease(path); 

     [self.layer addSublayer:shadowLayer]; 

我得到一個漂亮的白色內陰影,但是shadowLayer增加了黑色不透明矩形(陰影路徑)。

嘗試[shadowLayer setFillColor:UIColor.clearColor.CGColor];但它也消除了陰影。另外[shadowLayer setBackgroundColor:UIColor.redColor.CGColor];什麼都不做。

Example

我怎樣才能擺脫它?

回答

1

添加[self.layer setMasksToBounds:YES];解決了問題!