iOS UIView的一些方法和属性(本博客侧重于视图布局部分的学习)
好了,我接著上篇,開始我們的對(duì)UIView 實(shí)例方法的探索
UIView?實(shí)例方法 Instance Methods
初始化一個(gè)視圖
- (id)initWithFrame:(CGRect)aRect //用指定的frame 初始化一個(gè)視圖對(duì)象
結(jié)束視圖中的編輯狀態(tài)
- (BOOL)endEditing:(BOOL)force?
//這個(gè)在彈回鍵盤時(shí)特別有用
關(guān)于響應(yīng)
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
//為視圖添加一個(gè)手勢(shì)識(shí)別器
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
//移除一個(gè)手勢(shì)識(shí)別器
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//開始一個(gè)手勢(shì)識(shí)別器
- (void)addMotionEffect:(UIMotionEffect *)effect
//開始向視圖中添加運(yùn)動(dòng)效果,這么說其實(shí)比較模糊,比如說警告框會(huì)隨著手機(jī)的傾斜而傾斜,
當(dāng)然這需要代碼實(shí)現(xiàn),只是我們現(xiàn)在可以檢測(cè)手機(jī)的運(yùn)動(dòng)事件來做出相應(yīng)的響應(yīng)
//從IOS7開始使用
管理視圖層次 Managing the View Hierarchy
- (void)addSubview:(UIView *)view //添加子視圖
- (void)removeFromSuperview //從父視圖中移除
- (void)bringSubviewToFront:(UIView *)view?
//移動(dòng)指定的子視圖到最頂層
- (void)sendSubviewToBack:(UIView *)view
//移動(dòng)制定的子視圖到后方,所有子視圖的下面
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
//在指定的位置插入子視圖,視圖的所有視圖其實(shí)組成了一個(gè)數(shù)組
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
//將指定的子視圖移動(dòng)到指定siblingSubview子視圖的前面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
//將指定的子視圖移動(dòng)到指定siblingSubview子視圖的后面
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2
//交換兩子視圖的位置
- (BOOL)isDescendantOfView:(UIView *)view
//判斷接收對(duì)象是否是指定視圖的子視圖,或與指定視圖是同一視圖
配置自動(dòng)調(diào)整大小狀態(tài) Configuring the Resizing Behavior
- (void)sizeToFit //根據(jù)子視圖的大小位置,調(diào)整視圖,使其恰好圍繞子視圖,
也就是說自動(dòng)適應(yīng)子視圖的大小,只顯示子視圖
- (CGSize)sizeThatFits:(CGSize)size
//讓視圖計(jì)算最適合子視圖的大小,即能把全部子視圖顯示出來所需要的最小的size
鋪設(shè)子視圖 Laying out Subviews
- (void)layoutSubviews //勾畫子視圖
- (void)layoutIfNeeded //立即勾畫子視圖
- (void)setNeedsLayout?
//使當(dāng)前接收對(duì)象的布局作廢,并在下一更新周期觸發(fā)一個(gè)新的布局
選擇加入基于約束的布局 Opting in to Constraint-Based Layout
- (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag
//設(shè)置視圖自動(dòng)調(diào)整尺寸的掩碼是否轉(zhuǎn)化為基于約束布局的約束
- (BOOL)translatesAutoresizingMaskIntoConstraints
//判斷視圖是否將自動(dòng)調(diào)整尺寸的掩碼是否轉(zhuǎn)化為基于約束布局的約束
管理約束 Managing Constraints
- (NSArray *)constraints //返回由視圖創(chuàng)建的約束
- (void)addConstraint:(NSLayoutConstraint *)constraint
//為視圖布局或者子視圖添加一個(gè)約束,constraint約束只能在當(dāng)前視圖范圍內(nèi),包括子視圖
- (void)addConstraints:(NSArray *)constraints
//添加一組約束
- (void)removeConstraint:(NSLayoutConstraint *)constraint
//移除視圖上指定的約束
- (void)removeConstraints:(NSArray *)constraints
//移除指定的一組約束
在基于約束布局的測(cè)量 Measuring in Constraint-Based Layout
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize
//返回滿足持有約束的視圖的size
- (CGSize)intrinsicContentSize
//返回接收對(duì)象的原本大小
- (void)invalidateIntrinsicContentSize
//廢除視圖原本內(nèi)容的size
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis
//設(shè)置當(dāng)視圖要變小時(shí),視圖的壓縮改變方式,是水平縮小還是垂直縮小,并返回一個(gè)優(yōu)先權(quán)
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//設(shè)置優(yōu)先權(quán)
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis
//與上邊相反是視圖的放大改變方式
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//參照上面的方法
校準(zhǔn)視圖 Aligning Views with Constraint-Based Layout
- (CGRect)alignmentRectForFrame:(CGRect)frame
//返回給定框架的視圖的對(duì)齊矩陣
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect
//返回給定對(duì)齊矩形的視圖的frame
- (UIEdgeInsets)alignmentRectInsets
//返回從視圖的frame上定義的對(duì)齊矩陣的邊框?
- (UIView *)viewForBaselineLayout
//返回滿足基線約束條件的視圖
觸發(fā)基于約束的布局 Triggering Constraint-Based Layout
- (BOOL)needsUpdateConstraints //視圖的約束是否需要更新
- (void)setNeedsUpdateConstraints //設(shè)置視圖的約束需要更新
- (void)updateConstraints //為視圖更新約束
- (void)updateConstraintsIfNeeded //更新視圖和其子視圖的約束
調(diào)試基于約束的布局 Debugging Constraint-Based Layout
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis
//返回影響一個(gè)給定軸視圖布局的約束
- (BOOL)hasAmbiguousLayout //視圖的位置是否不完全指定
- (void)exerciseAmbiguityInLayout
//在不同的有效值之間用一個(gè)模糊的布局隨機(jī)改變視圖的frame
繪畫和更新視圖 Drawing and Updating the View
- (void)drawRect:(CGRect)rect //在指定的區(qū)域繪畫視圖
- (void)setNeedsDisplay //標(biāo)記整個(gè)視圖的邊界矩形需要重繪
- (void)setNeedsDisplayInRect:(CGRect)invalidRect
//標(biāo)記在指定區(qū)域內(nèi)的視圖的邊界需要重繪
格式化打印視圖內(nèi)容
- (UIViewPrintFormatter *)viewPrintFormatter //返回視圖的打印格式化
- (void)drawRect:(CGRect)area forViewPrintFormatter:(UIViewPrintFormatter *)formatter
//指定區(qū)域和打印格式繪畫視圖內(nèi)容
保存和恢復(fù)狀態(tài) Preserving and Restoring State
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
//編碼視圖的狀態(tài)信息
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
//解碼一個(gè)視圖狀態(tài)信息
標(biāo)記視圖?
- (UIView *)viewWithTag:(NSInteger)tag
轉(zhuǎn)換視圖間坐標(biāo) Converting Between View Coordinate Systems
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
//轉(zhuǎn)換一個(gè)點(diǎn)從接受對(duì)象的坐標(biāo)系到指定視圖
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
//與上面相反,指定視圖坐標(biāo)中的一個(gè)點(diǎn)轉(zhuǎn)換為接收對(duì)象
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view
//參照上面兩個(gè)方法
視圖中的點(diǎn)擊測(cè)試
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//在指定點(diǎn)上點(diǎn)擊測(cè)試指定事件
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
//測(cè)試指定的點(diǎn)是否包含在接收對(duì)象中
注意與視圖相關(guān)聯(lián)的改變 Observing View-Related Changes
- (void)didAddSubview:(UIView *)subview
//通知視圖指定子視圖已經(jīng)添加
- (void)willRemoveSubview:(UIView *)subview
//通知視圖將要移除指定的子視圖
- (void)willMoveToSuperview:(UIView *)newSuperview
//通知視圖將要移動(dòng)到一個(gè)新的父視圖中
- (void)didMoveToSuperview
//通知視圖已經(jīng)移動(dòng)到一個(gè)新的父視圖中
- (void)willMoveToWindow:(UIWindow *)newWindow
//通知視圖將要移動(dòng)到一個(gè)新的window中
- (void)didMoveToWindow
//通知視圖已經(jīng)移動(dòng)到一個(gè)新的window中
轉(zhuǎn)載于:https://www.cnblogs.com/wfwenchao/articles/3831696.html
總結(jié)
以上是生活随笔為你收集整理的iOS UIView的一些方法和属性(本博客侧重于视图布局部分的学习)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TCP/IP学习笔记-如何理解
- 下一篇: Csharp volatile 关键字