您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

全面掌握 UIView 實例方法(Instance Methods)

編輯:iOS 相關資訊

緊接上一篇,這次我們把視角拉到 UIView 的實例方法上。這個類別的方法多到令人眼花,但只要按功能分類理解,就能建立起清晰的知識地圖,日後查文件、寫程式都不必再走冤枉路。

初始化與編輯狀態

- (id)initWithFrame:(CGRect)aRect   // 以指定 frame 初始化視圖物件
- (BOOL)endEditing:(BOOL)force       // 結束視圖中的編輯狀態,彈回鍵盤時特別好用

關於響應

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer    // 加入手勢識別器
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer  // 移除此手勢識別器
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer  // 判斷是否開始手勢識別
- (void)addMotionEffect:(UIMotionEffect *)effect   // 為視圖加入運動效果(iOS 7 起)

有了 addMotionEffect:,像是警告框隨手機傾斜而傾斜這類效果,就能透過偵測裝置的運動事件來實作。

管理視圖層次(Managing the View Hierarchy)

- (void)addSubview:(UIView *)view            // 加入子視圖
- (void)removeFromSuperview                  // 從父視圖中移除
- (void)bringSubviewToFront:(UIView *)view   // 移到最頂層
- (void)sendSubviewToBack:(UIView *)view     // 移到所有子視圖的後方
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index               // 插入到指定位置
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview  // 移到指定子視圖前面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview  // 移到指定子視圖後面
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2  // 交換兩個子視圖位置
- (BOOL)isDescendantOfView:(UIView *)view    // 判斷是否為指定視圖的子視圖或同一視圖

由於視圖的所有子視圖其實組成了一個陣列,因此才能用 index 來插入與交換。

配置自動縮放(Configuring the Resizing Behavior)

- (void)sizeToFit              // 依子視圖大小位置調整自身,恰好環繞子視圖
- (CGSize)sizeThatFits:(CGSize)size  // 計算能完整顯示所有子視圖所需的最小 size

鋪設子視圖(Laying out Subviews)

- (void)layoutSubviews     // 勾畫子視圖
- (void)layoutIfNeeded     // 立即勾畫子視圖
- (void)setNeedsLayout     // 使目前布局作廢,並在下一更新週期觸發新布局

基於約束的布局(Constraint-Based Layout)

選擇加入

- (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag  // 是否將自動縮放遮罩轉為約束
- (BOOL)translatesAutoresizingMaskIntoConstraints

管理約束

- (NSArray *)constraints                                    // 回傳由視圖建立的約束
- (void)addConstraint:(NSLayoutConstraint *)constraint       // 加入單一約束
- (void)addConstraints:(NSArray *)constraints               // 加入一組約束
- (void)removeConstraint:(NSLayoutConstraint *)constraint    // 移除指定約束
- (void)removeConstraints:(NSArray *)constraints            // 移除一組約束

測量

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize  // 回傳滿足持有約束的 size
- (CGSize)intrinsicContentSize                            // 回傳物件本身固有大小
- (void)invalidateIntrinsicContentSize                    // 作废旧有內容大小
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis  // 視圖變小時的壓縮優先權
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis               // 視圖變大時的抗張優先權
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis

對齊視圖

- (CGRect)alignmentRectForFrame:(CGRect)frame   // 回傳給定框架的對齊矩形
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect  // 回傳給定對齊矩形的 frame
- (UIEdgeInsets)alignmentRectInsets             // 回傳對齊矩形相對於 frame 的邊框
- (UIView *)viewForBaselineLayout               // 回傳滿足基線約束的視圖

觸發與偵錯

- (BOOL)needsUpdateConstraints              // 約束是否需要更新
- (void)setNeedsUpdateConstraints            // 標記約束需要更新
- (void)updateConstraints                    // 更新視圖約束
- (void)updateConstraintsIfNeeded            // 更新視圖與子視圖的約束
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis  // 回傳影響指定軸布局的約束
- (BOOL)hasAmbiguousLayout                   // 視圖位置是否未被完全指定
- (void)exerciseAmbiguityInLayout            // 隨機改變 frame 以測試模糊布局

繪製與更新視圖

- (void)drawRect:(CGRect)rect                        // 在指定區域繪製視圖
- (void)setNeedsDisplay                                // 標記整個視圖邊界需重繪
- (void)setNeedsDisplayInRect:(CGRect)invalidRect      // 標記指定區域需重繪

格式化列印與狀態保存

- (UIViewPrintFormatter *)viewPrintFormatter                                  // 回傳視圖的列印格式化
- (void)drawRect:(CGRect)area forViewPrintFormatter:(UIViewPrintFormatter *)formatter  // 依列印格式繪製
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder   // 編碼視圖狀態
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder   // 解碼視圖狀態
- (UIView *)viewWithTag:(NSInteger)tag                    // 依 tag 標記並取得視圖

座標系轉換

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view     // 將點轉為指定視圖座標
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view   // 將指定視圖的點轉為自身座標
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view

點擊測試

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event   // 在指定點做點擊測試
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event    // 測試點是否落在物件內

觀察視圖相關變化

- (void)didAddSubview:(UIView *)subview           // 通知已加入子視圖
- (void)willRemoveSubview:(UIView *)subview        // 通知將移除子視圖
- (void)willMoveToSuperview:(UIView *)newSuperview  // 通知將移到新父視圖
- (void)didMoveToSuperview                          // 通知已移到新父視圖
- (void)willMoveToWindow:(UIWindow *)newWindow     // 通知將移到新 window
- (void)didMoveToWindow                             // 通知已移到新 window

以上便是 UIView 的實例方法全貌。數量雖多,但只要按官方文件的分類逐塊攻克,你就能對這個核心類別了然於胸,之後開發時自然游刃有餘。

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