Quartz 2D Programming Guide –Transparency Layers

來源:互聯網
上載者:User

參考

[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,效果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.