IOS UIView 之實例方法篇 Instance Methods

好了,我接着上篇,開始我們的對UIView 實例方法的探索
UIView 實例方法  Instance Methods

初始化一個視圖
- (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
//開始向視圖中添加運動效果,這麼說其實比較模糊,比如說警告框會隨着手機的傾斜而傾斜,
   當然這需要代碼實現,只是我們現在可以檢測手機的運動事件來做出相應的響應
//從IOS7開始使用

管理視圖層次  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
//將指定的子視圖移動到指定siblingSubview子視圖的前面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
//將指定的子視圖移動到指定siblingSubview子視圖的後面
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2
//交換兩子視圖的位置

- (BOOL)isDescendantOfView:(UIView *)view
//判斷接收對象是否是指定視圖的子視圖,或與指定視圖是同一視圖

配置自動調整大小狀態  Configuring the Resizing Behavior
- (void)sizeToFit  //根據子視圖的大小位置,調整視圖,使其恰好圍繞子視圖,
                                  也就是說自動適應子視圖的大小,只顯示子視圖
- (CGSize)sizeThatFits:(CGSize)size
//讓視圖計算最適合子視圖的大小,即能把全部子視圖顯示出來所需要的最小的size

鋪設子視圖   Laying out Subviews

- (void)layoutSubviews  //勾畫子視圖
- (void)layoutIfNeeded  //立即勾畫子視圖
- (void)setNeedsLayout 
//使當前接收對象的佈局作廢,並在下一更新週期觸發一個新的佈局

選擇加入基於約束的佈局  Opting in to Constraint-Based Layout

- (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag
//設置視圖自動調整尺寸的掩碼是否轉化爲基於約束佈局的約束
- (BOOL)translatesAutoresizingMaskIntoConstraints
//判斷視圖是否將自動調整尺寸的掩碼是否轉化爲基於約束佈局的約束

管理約束  Managing Constraints

- (NSArray *)constraints  //返回由視圖創建的約束

- (void)addConstraint:(NSLayoutConstraint *)constraint
//爲視圖佈局或者子視圖添加一個約束,constraint約束只能在當前視圖範圍內,包括子視圖
- (void)addConstraints:(NSArray *)constraints
//添加一組約束

- (void)removeConstraint:(NSLayoutConstraint *)constraint
//移除視圖上指定的約束
- (void)removeConstraints:(NSArray *)constraints
//移除指定的一組約束

在基於約束佈局的測量  Measuring in Constraint-Based Layout

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize
//返回滿足持有約束的視圖的size

- (CGSize)intrinsicContentSize
//返回接收對象的原本大小

- (void)invalidateIntrinsicContentSize
//廢除視圖原本內容的size

- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis
//設置當視圖要變小時,視圖的壓縮改變方式,是水平縮小還是垂直縮小,並返回一個優先權
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//設置優先權

- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis
//與上邊相反是視圖的放大改變方式
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//參照上面的方法

校準視圖  Aligning Views with Constraint-Based Layout

- (CGRect)alignmentRectForFrame:(CGRect)frame
//返回給定框架的視圖的對齊矩陣
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect
//返回給定對齊矩形的視圖的frame
- (UIEdgeInsets)alignmentRectInsets
//返回從視圖的frame上定義的對齊矩陣的邊框 
- (UIView *)viewForBaselineLayout
//返回滿足基線約束條件的視圖

觸發基於約束的佈局  Triggering Constraint-Based Layout

- (BOOL)needsUpdateConstraints
//視圖的約束是否需要更新
- (void)setNeedsUpdateConstraints
 //設置視圖的約束需要更新

- (void)updateConstraints  
//爲視圖更新約束
- (void)updateConstraintsIfNeeded
 //更新視圖和其子視圖的約束

調試基於約束的佈局  Debugging Constraint-Based Layout

- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis
//返回影響一個給定軸視圖佈局的約束

- (BOOL)hasAmbiguousLayout  
//視圖的位置是否不完全指定

- (void)exerciseAmbiguityInLayout
//在不同的有效值之間用一個模糊的佈局隨機改變視圖的frame

繪畫和更新視圖  Drawing and Updating the View

- (void)drawRect:(CGRect)rect  
//在指定的區域繪畫視圖

- (void)setNeedsDisplay
 //標記整個視圖的邊界矩形需要重繪
- (void)setNeedsDisplayInRect:(CGRect)invalidRect
//標記在指定區域內的視圖的邊界需要重繪

格式化打印視圖內容

- (UIViewPrintFormatter *)viewPrintFormatter
//返回視圖的打印格式化

- (void)drawRect:(CGRect)area forViewPrintFormatter:(UIViewPrintFormatter *)formatter
//指定區域和打印格式繪畫視圖內容

保存和恢復狀態  Preserving and Restoring State

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
//編碼視圖的狀態信息
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
//解碼一個視圖狀態信息

標記視圖  
- (UIView *)viewWithTag:(NSInteger)tag

轉換視圖間座標  Converting Between View Coordinate Systems

- (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
//測試指定的點是否包含在接收對象中

注意與視圖相關聯的改變  Observing View-Related Changes

- (void)didAddSubview:(UIView *)subview
//通知視圖指定子視圖已經添加
- (void)willRemoveSubview:(UIView *)subview
//通知視圖將要移除指定的子視圖

- (void)willMoveToSuperview:(UIView *)newSuperview
//通知視圖將要移動到一個新的父視圖中
- (void)didMoveToSuperview
//通知視圖已經移動到一個新的父視圖中

- (void)willMoveToWindow:(UIWindow *)newWindow
//通知視圖將要移動到一個新的window中
- (void)didMoveToWindow
//通知視圖已經移動到一個新的window中

以上即是UIView 類的實例方法,比較多,我按照文檔把它分爲了幾類。

到此爲止這個類差不多已經瞭解了大概,正如我所期望的那樣,要知道
這個類中都有什麼東西,以後不至於走很多彎路。

非常歡迎對我的文章提出建議,或指正錯誤,讓我們共同進步 —— LC




發佈了56 篇原創文章 · 獲贊 4 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章