參考
[1]http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_trans_layers/dq_trans_layers.html#//apple_ref/doc/uid/TP30001066-CH210-TPXREF101
透明層的一個作用當對多個圖形應用陰影時,能夠實現多個圖形的完整陰影,而不是幾個陰影疊加。
應用透明層的陰影如下:
如果未應用陰影層時,效果如下:
透明層跟普通的層一樣,都是獨立的實體。quartz的每個context包含一個透明層stack,並且可以嵌套,但因為層經常是stack 的一部分,所以一般不能直接操作他們。
使用透明層的步驟
1.調用CGContextBeginTransparencyLayer,這之後graphic context的alpha自動化佈建為1,關閉陰影,blend mode 設定為預設,其他參數未變。
2.在context的其他自訂其他繪製操作。
3.調用CGContextEndTransparencyLayer,quartz最後彙總結果到context中。
以下是使用透明層的代碼,在drawRect中調用
- (void)drawRect:(CGRect)rect{[self initTransparencyLayer];}
- (void)initTransparencyLayer{CGFloat size = 100;CGRect rect = CGRectMake(0, 0, 300, 300);CGRect redRect = CGRectMake(10, 10, size, size);CGRect greenRect = CGRectMake(size/2, size/2, size, size);CGRect blueRect = CGRectMake(size, size, size, size);CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetShadow(context, CGSizeMake(5, 10), 10);CGContextBeginTransparencyLayerWithRect(context, rect, NULL);CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);CGContextFillEllipseInRect(context, redRect);CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);CGContextFillEllipseInRect(context, greenRect);CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);CGContextFillEllipseInRect(context, blueRect);CGContextEndTransparencyLayer(context);}
效果如下:
設定CGContextSetShadow(context, CGSizeMake(5, 10), 0);第三個參數,模糊參數為0,效果如下: