您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

透視 UIView(一):屬性全解析

編輯:iOS 開發綜合

從入門到現在,操作過的 Button、Label、TextField 全都是 UIView 的後代。這個類別的官方文件有一百多頁,我打算分幾篇寫完,本篇先談屬性。

UIView 繼承自 UIResponder,遵循 NSCoding、UIAppearance、UIAppearanceContainer、UIDynamicItem、NSObject 等協議。從它繼承自 UIResponder 這點就能看出:UIView 本身具備響應手勢與事件的能力。

外觀與可見性

@property(nonatomic) CGFloat alpha;              // 透明度,0.0 全透明 ~ 1.0 全不透明
                                                 // 只影響自身,不影響子視圖的 alpha 值
@property(nonatomic, getter=isOpaque) BOOL opaque;             // 是否為不透明視圖
@property(nonatomic, copy) UIColor *backgroundColor;           // 背景色
@property(nonatomic, getter=isHidden) BOOL hidden;             // 是否隱藏
@property(nonatomic) CGFloat contentScaleFactor;               // 內容縮放比例

alpha 與 hidden 的差異值得留意:hidden 設為 YES 時視圖完全不參與繪製,也不接收觸控;alpha 設為 0 時視圖仍然「存在」,只是看不見。另外把 opaque 正確設為 YES 能讓系統跳過背景合成,滾動效能會更好。

座標與尺寸

@property(nonatomic) CGRect bounds;    // 自身座標系下的邊界(原點通常為 0)
@property(nonatomic) CGRect frame;     // 父視圖座標系下的位置與大小
@property(nonatomic) CGPoint center;   // 父視圖座標系下的中心點

frame 與 bounds 的分工是新手最容易混淆的地方:frame 是「我在父視圖的哪裡」,bounds 是「我自己的座標系長什麼樣」。當 transform 不為 identity 時,frame 會變成推算出來的數值,此時應該用 bounds 與 center 來定位。

自動縮放

@property(nonatomic) BOOL autoresizesSubviews;               // 自身 resize 時子視圖是否跟著調整
@property(nonatomic) UIViewAutoresizing autoresizingMask;    // 父視圖變化時自身如何調整

繪製相關

@property(nonatomic) BOOL clearsContextBeforeDrawing;   // 重畫前是否先清空,預設 YES
@property(nonatomic) BOOL clipsToBounds;                // 子視圖是否被裁切在自身 bounds 內,預設 NO
@property(nonatomic) UIViewContentMode contentMode;     // bounds 改變時內容的呈現方式

clearsContextBeforeDrawing 若設為 NO,你必須確保 drawRect: 裡的繪製是完整覆蓋的;在已經做過大量優化的情況下,這樣做能提升效能,特別是滾動時可能只需重繪部分區域。

事件與手勢

@property(nonatomic, getter=isExclusiveTouch) BOOL exclusiveTouch;
                    // 是否為處理觸摸的唯一對象(獨佔觸控)
@property(nonatomic, copy) NSArray *gestureRecognizers;      // 附加的手勢識別器
@property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled;
                    // 是否接受多點觸控,預設 NO
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
                    // 是否開啟使用者互動

階層與標識

@property(nonatomic, readonly, copy) NSArray *subviews;      // 所有子視圖
@property(nonatomic, readonly) UIView *superview;            // 父視圖
@property(nonatomic) NSInteger tag;                          // 標籤
@property(nonatomic, readonly, retain) CALayer *layer;       // 渲染用的圖層
@property(nonatomic, readonly) UIWindow *window;             // 所屬的 UIWindow

其他

@property(nonatomic) CGAffineTransform transform;            // 仿射變換
@property(nonatomic, copy) NSString *restorationIdentifier;  // 狀態恢復標識
@property(copy, nonatomic) NSArray *motionEffects;           // 運動效果,iOS 7 起
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode;   // 色調調整模式,iOS 7 起
@property(nonatomic, retain) UIColor *tintColor;                    // 色調顏色,iOS 7 起

restorationIdentifier 的作用類似 table view cell 的 reuse identifier:它決定這個視圖是否參與狀態恢復。要注意的是,不能只幫某個視圖設這個值——它對應的 view controller,以及所有父層 view controller 都必須具備恢復標識,否則整體機制不會生效。

以上就是 UIView 的屬性。下一篇接著整理它的類方法與動畫 API。

轉接下篇 UIView 之方法篇:https://blog.csdn.net/lc_obj/article/details/18511511

這裡只是把 UIView 類的屬性分享給大家,若有錯誤歡迎指正 —— LC

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved