2010-05-23 29 views
2

我正在使用帶有路徑的CAShapeLayer。現在我希望它能夠投射出約10個單位厚度的光滑陰影。如何使CAShapeLayer在iPhone OS 3.0中投影?

第一:是的,我可以創建11個CAShapeLayer對象,並且每次在每次迭代中增加1個單元的路徑輪廓,使用不同的顏色和更多的alpha。但是這樣我就會炸掉我的內存空間,因爲這個東西只有一半的屏幕大小,這意味着11x的內存中有一半的屏幕大小的位圖。

因此,自iPhone OS 3.2以來,我可能可以在CALayer上使用這些漂亮的陰影屬性。但我想堅持OS 3.0。那麼除了上面討厭的那個之外,我還有什麼選擇?

+0

我不知道有什麼好處呢的CAShapeLayer爲您提供在這種情況下,也許你會得到更好的繪圖使用核芯顯卡與陰影設置爲你喜歡的路徑自己。 – Palimondo 2011-05-19 00:07:26

回答

0

您可以使用Core Graphics創建陰影。您需要的構建塊在QuartzDemo示例中進行了說明。特別看看class QuartzMaskingViewQuartzClipping.m

  1. 捕捉形狀圖層的內容轉換成圖像
  2. 設置陰影根據自己的喜好
  3. 開始透明層
  4. 夾到的圖層內容的圖像 - 你會被繪製的它
  5. 畫出你的形象再次

這導致陰影正在繪製你的隱藏區域之外。

CGSize size = CGSizeMake(300, 100); 

UIGraphicsBeginImageContextWithOptions(size,NO, 0.0); 
[shapeLayer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

CGRect flippedImageRect = 
    CGRectMake(0, 0, image.size.width, -image.size.height); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(ctx); 
CGContextSetShadowWithColor(ctx, CGSizeMake(4, 4), 2, 
    [[UIColor colorWithWhite:0 alpha:0.4] CGColor]); 
CGContextBeginTransparencyLayer(ctx, NULL); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGContextClipToMask(ctx, flippedImageRect, [image CGImage]); 
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]); 
CGContextDrawImage(ctx, flippedImageRect, [image CGImage]); 
CGContextEndTransparencyLayer(ctx); 
CGContextRestoreGState(ctx);