iOS開發-Alpha,Hidden與Opaque區別

來源:互聯網
上載者:User

標籤:

UIView中的這三個屬性用的比較多,尤其是Alpha和Opaque之間有的時候不是很好分別,稍微整理下:

Alpha(不透明度)

alpha是不透明度,屬性為浮點類型的值,取值範圍從0到1.0,表示從完全透明到完全不透明,其特性有當前UIView的alpha值會被其所有subview繼承。alpha值會影響到UIView跟其所有subview,alpha具有動畫效果。當alpha為0時,跟hidden為YES時效果一樣,但是alpha主要用於實現隱藏的動畫效果,在動畫塊中將hidden設定為YES沒有動畫效果。

 

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(30, 100, CGRectGetWidth(self.view.bounds)-60, 150)];    [view setBackgroundColor:[UIColor redColor]];    [view setAlpha:0.5];    [self.view addSubview:view];        UIView *childView=[[UIView alloc]initWithFrame:CGRectMake(20, 30, 100, 80)];    [childView setBackgroundColor:[UIColor blueColor]];    [view addSubview:childView];

 

設定backgroundColor的alpha值隻影響當前UIView的背景,並不會影響其所有subview。Clear Color就是backgroundColor的alpha為1.0。alpha值會影響backgroundColor最終的alpha,假設UIView的alpha為0.8,backgroundColor的alpha為0.5,那麼backgroundColor最終的alpha為0.4(0.8*0.5)。

Hidden(隱藏)

Hidden表示UIView是否隱藏,Hidden設定為YES表示當前UIView的所有subview也會被隱藏,忽略subview的hidden屬性。Hidden只要設定為YES,所有的subview都會隱藏。UIView隱藏之後也會從當前的響應者事件中移除。

Opaque

opaque也是表示當前的UIView的不透明度,設定是否之後對於UIView的顯示並沒有什麼影響,官方文檔的意思簡單點說就是opaque預設為YES,如果alpha小於1,那麼應該設定opaque設定為NO,但是如果alpha為1,opaque設定為NO,產生的後果是不可預料的~

 

This property provides a hint to the drawing system as to how it should treat the view. If set to YES, the drawing system treats the view as fully opaque, which allows the drawing system to optimize some drawing operations and improve performance. If set to NO, the drawing system composites the view normally with other content. The default value of this property is YES.An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.You only need to set a value for the opaque property for subclasses of UIView that draw their own content using the drawRect: method. The opaque property has no effect for system provided classes such as UIButton, UILabel, UITableViewCell, etc.

 

如果瞭解opaque,需要點螢幕繪製的知識,螢幕上的每個像素點都是通過RGBA值(Red、Green、Blue三原色再配上Alpha透明度)表示的,當紋理(UIView在繪圖系統中對應的表示項)出現重疊時,GPU會按照Result = Source + Destination * (1 - SourceAlpha)公式計算重疊部分的像素。

Result是結果RGB值,Source為處在重疊頂部紋理的RGB值,Destination為處在重疊底部紋理的RGB值。

當SourceAlpha為1時,繪圖系統認為下面的顏色全部被遮蓋住了,Result=Source,如果Source的Alpha不為0,上下層顏色就會進行合成,所以opaque預設設定YES,提升繪製效能,如果開發中UIView是不透明的,opaque設定為YES, 如果opaque設定NO,那麼Alpha應該小於1.

參考資料:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/opaque

iOS開發-Alpha,Hidden與Opaque區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.