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,減小開銷
相關文章
相關標籤/搜索