iOS開發——View的透明屬性hidden、alpha、opaque


Hidden、Alpha、Opaque的區別

在iOS中,每個View都有Hidden、Alpha、Opaque三個關於透明的屬性,官方文檔介紹如下:

1. @property(nonatomic) CGFloat alpha;
This value affects only the current view and does not affect any of its embedded subviews.Changes to this property can be animated.
2. @property(nonatomic, getter=isHidden) BOOL hidden;
The default value is NO.
A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual.Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants.
3. @property(nonatomic, getter=isOpaque) BOOL opaque;
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.
You should always set the value of this property to NO if the view is fully or partially transparent.

大概意思如下:

  1. alpha
    • 這個屬性只能影響當前視圖,不能連帶影響子視圖
    • 可以當做動畫進行動態改變
    • alpha = 0 時仍舊接收事件,但是這個操作比Hidden開銷大
  2. hidden
    • 默認為NO,就是顯示狀態
    • hidden的視圖不再接收事件
    • hidden的視圖仍然在父視圖的子視圖列表里,而且響應自適應autoresizing的事件
    • hidden的視圖所有子視圖也會被Hidden而且它們的Hidden屬性不會被改變
  3. opaque
    • 如果opaque設置為YES,那么視圖會被當做全視圖來對待,系統會重繪整個視圖
    • 如果opaque設置為NO,那么系統會減少開銷,以其中的內容來判定重繪的視圖
    • 如果把視圖的背景色設置為透明那個,那么opaque最好設置為NO,減少開銷


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM