2013-05-13 30 views
1

我有一個陰影圖像,我想圍繞分組的UITableView部分的外邊緣繪製。這是圖像:我可以使用UIBezierPath在UITableView部分周圍繪製陰影圖像嗎?

enter image description here

我可以代表我想提請周圍的矩形的UIBezierPath,但我無法弄清楚如何沿着矩形的路線重複圖像。到目前爲止,它只是用圖像填充矩形:

UIImage *patternImg = [UIImage imageNamed:@"cellShadow"]; 
UIColor *fill = [UIColor colorWithPatternImage:patternImg]; 
[fill setFill]; 
CGRect aSectRect = [self rectForSection:0]; 
UIBezierPath *aSectPath = [self createRoundedPath:aSectRect]; 
[aSectPath fill]; 

這可能嗎?我需要做什麼?

回答

3

不幸的是,沒有辦法讓UIBezierPath使用圖像作爲「刷子」,這基本上是你想要的。但你可以有CoreGraphics中繪製一個陰影給你:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetShadow(context, CGSizeZero, myShadowRadius); 
// Draw your shape here 

現在,如果你畫只有一個形狀是會得到一個陰影。但是如果你畫出更多的形狀,每一個都會得到自己的影子,這可能不是你想要的。該解決方案被稱爲透明層,這是不相關的CALayers或東西,但僅僅是某種CoreGraphics在「分組」的:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetShadow(context, CGSizeZero, myShadowRadius); 
CGContextBeginTransparencyLayer(context, NULL); 
// Draw your shapes here. 
CGContextEndTransparencyLayer(context); 

CGContextBeginTransparencyLayerCGContextEndTransparencyLayer之間的呼叫影子被禁用。在CGContextEndTransparencyLayer調用之後,將陰影應用於在開始結束之間繪製的所有內容,就好像它只是一個形狀一樣。