在iOS開發中,我們知道有一個共同的基類——NSObject,但是對於界面視圖而言,UIView是非常重要的一個類,UIView是很多視圖控制項的基類,因此,對於UIView的學習閑的非常有必要。在iOS學習——iOS 整體框架及類繼承框架圖中列出了iOS中所有類的繼承框架圖,其中下麵這張圖就是iOS ...
在iOS開發中,我們知道有一個共同的基類——NSObject,但是對於界面視圖而言,UIView是非常重要的一個類,UIView是很多視圖控制項的基類,因此,對於UIView的學習閑的非常有必要。在iOS學習——iOS 整體框架及類繼承框架圖中列出了iOS中所有類的繼承框架圖,其中下麵這張圖就是iOS開發中的界面相關類的繼承框架圖。
下麵主要通過學習UIView.h文件來瞭解UIView主要提供了那些方法和屬性,從UIView.h的源碼來看,UIView.h的結構主要分為4個部分:
- 常用枚舉類型的定義,主要包括
- 視圖動畫曲線 UIViewAnimationCurve
- 視圖內容填充模式 UIViewContentMode
- 視圖動畫過渡效果 UIViewAnimationTransition
- 視圖自動調整大小方式 UIViewAutoresizing
- 視圖的動畫選項 UIViewAnimationOptions
- 視圖關鍵幀動畫選項 UIViewKeyframeAnimationOptions
- 視圖的系統動畫 UISystemAnimation
- 視圖的外觀色調調整模式 UIViewTintAdjustmentMode
- 語義內容屬性 UISemanticContentAttribute
- 佈局約束的軸 UILayoutConstraintAxis(水平還是豎直)(該定義在後面第四部分中間)
- 坐標空間協議UICoordinateSpace的定義,定義了坐標點(CGPoint)、坐標區域(CGRect)在兩個view間的轉換
- UIView的主要方法和屬性的定義
- 類方法 2個
- 初始化方法 2個
- 屬性 8個
- UIView的各類擴展
- 視圖幾何相關的擴展 UIView (UIViewGeometry),主要定義了視圖上位置和區域相關一些屬性和方法
- 視圖層次結構相關的擴展 UIView (UIViewHierarchy),主要定義對子視圖的增刪改以及層次結構調整等操作
- 視圖外觀渲染相關的擴展 UIView (UIViewRendering),主要定義視圖的一些屬性和設置,例如是否隱藏、透明度、背景顏色等
- 視圖動畫相關的擴展 UIView (UIViewAnimation) ,主要定義視圖上自定義一個動畫所需的一系列方法
- 視圖用block快速定義動畫的擴展 UIView (UIViewAnimationWithBlocks),主要提供7個類方法直接用block添加動畫,根據需要選擇不同的類方法進行相關參數的設定
- 視圖關鍵幀動畫相關的擴展 UIView (UIViewKeyframeAnimations) ,主要提供兩個類方法進行關鍵幀動畫的設定,也是直接採用block的方式實現
- 視圖上手勢相關的擴展 UIView (UIViewGestureRecognizers) ,主要提供添加、移除和手勢開始前回調3個方法
- 視圖上運動效果相關的擴展 UIView (UIViewMotionEffects),主要提供添加和移除運動效果兩個方法,還提供一個獲取視圖上所有運動效果的屬性
- 視圖上安裝約束相關的擴展 UIView (UIConstraintBasedLayoutInstallingConstraints) ,主要提供添加單個/多個、移除單個/多個約束的方法
- 視圖上約束相關的擴展 UIView (UIConstraintBasedLayoutCoreMethods),主要提供4種約束更新的方法
- 視圖上約束共存相關的擴展 UIView (UIConstraintBasedCompatibility)
- 視圖約束佈局圖層相關的擴展 IView (UIConstraintBasedLayoutLayering),主要是獲取視圖上約束相關的一些屬性,比喻第一視圖、基線視圖、視圖大小等等
- 視圖約束適應尺寸相關的擴展 UIView (UIConstraintBasedLayoutFittingSize),主要用於獲取持有約束的視圖大小及其區域內的視圖大小
- 視圖佈局引導相關的擴展 UIView (UILayoutGuideSupport),主要定義了視圖佈局所需的一些屬性,類似中心點、上下左右寬高等屬性
- 視圖約束佈局調試相關的擴展 UIView (UIConstraintBasedLayoutDebugging)
- 視圖快照相關的擴展 UIView (UISnapshotting)
下麵是UIView.h文件的源碼分析,以下內容參考自:ios開發 之 UIView詳解。
1 #import <Foundation/Foundation.h> 2 #import <QuartzCore/QuartzCore.h> 3 #import <UIKit/UIResponder.h> 4 #import <UIKit/UIInterface.h> 5 #import <UIKit/UIKitDefines.h> 6 #import <UIKit/UIAppearance.h> 7 #import <UIKit/UIDynamicBehavior.h> 8 #import <UIKit/NSLayoutConstraint.h> 9 #import <UIKit/UITraitCollection.h> 10 #import <UIKit/UIFocus.h> 11 12 NS_ASSUME_NONNULL_BEGIN 13 14 /** 動畫的曲線枚舉 */ 15 typedef NS_ENUM(NSInteger, UIViewAnimationCurve) { 16 UIViewAnimationCurveEaseInOut, //!< 慢進慢出(預設值). 17 UIViewAnimationCurveEaseIn, //!< 慢進. 18 UIViewAnimationCurveEaseOut, //!< 慢出. 19 UIViewAnimationCurveLinear, //!< 勻速. 20 }; 21 22 //!< UIView內容填充模式. 23 typedef NS_ENUM(NSInteger, UIViewContentMode) { 24 UIViewContentModeScaleToFill, //!< 縮放內容到合適比例大小. 25 UIViewContentModeScaleAspectFit, //!< 縮放內容到合適的大小,邊界多餘部分透明. 26 UIViewContentModeScaleAspectFill, //!< 縮放內容填充到指定大小,邊界多餘的部分省略. 27 UIViewContentModeRedraw, //!< 重繪視圖邊界 (需調用 -setNeedsDisplay). 28 UIViewContentModeCenter, //!< 視圖保持等比縮放. 29 UIViewContentModeTop, //!< 視圖頂部對齊. 30 UIViewContentModeBottom, //!< 視圖底部對齊. 31 UIViewContentModeLeft, //!< 視圖左側對齊. 32 UIViewContentModeRight, //!< 視圖右側對齊. 33 UIViewContentModeTopLeft, //!< 視圖左上角對齊. 34 UIViewContentModeTopRight, //!< 視圖右上角對齊. 35 UIViewContentModeBottomLeft, //!< 視圖左下角對齊. 36 UIViewContentModeBottomRight, //!< 視圖右下角對齊. 37 }; 38 39 /** UIView動畫過渡效果 */ 40 typedef NS_ENUM(NSInteger, UIViewAnimationTransition) { 41 UIViewAnimationTransitionNone, //!< 無效果. 42 UIViewAnimationTransitionFlipFromLeft, //!< 沿視圖垂直中心軸左到右移動. 43 UIViewAnimationTransitionFlipFromRight, //!< 沿視圖垂直中心軸右到左移動. 44 UIViewAnimationTransitionCurlUp, //!< 由底部向上捲起. 45 UIViewAnimationTransitionCurlDown, //!< 由頂部向下展開. 46 }; 47 48 /** 自動調整大小方式 */ 49 typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) { 50 UIViewAutoresizingNone = 0, //!< 不自動調整. 51 UIViewAutoresizingFlexibleLeftMargin = 1 << 0,//!< 自動調整與superView左邊的距離,保證與superView右邊的距離不變. 52 UIViewAutoresizingFlexibleWidth = 1 << 1,//!< 自動調整自己的寬度,保證與superView左邊和右邊的距離不變. 53 UIViewAutoresizingFlexibleRightMargin = 1 << 2,//!< 自動調整與superView的右邊距離,保證與superView左邊的距離不變. 54 UIViewAutoresizingFlexibleTopMargin = 1 << 3,//!< 自動調整與superView頂部的距離,保證與superView底部的距離不變. 55 UIViewAutoresizingFlexibleHeight = 1 << 4,//!< 自動調整自己的高度,保證與superView頂部和底部的距離不變. 56 UIViewAutoresizingFlexibleBottomMargin = 1 << 5 //!< 自動調整與superView底部的距離,也就是說,與superView頂部的距離不變. 57 }; 58 59 /** UIView動畫選項 */ 60 typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) { 61 UIViewAnimationOptionLayoutSubviews = 1 << 0, //!< 動畫過程中保證子視圖跟隨運動. 62 UIViewAnimationOptionAllowUserInteraction = 1 << 1, //!< 動畫過程中允許用戶交互. 63 UIViewAnimationOptionBeginFromCurrentState = 1 << 2, //!< 所有視圖從當前狀態開始運行. 64 UIViewAnimationOptionRepeat = 1 << 3, //!< 重覆運行動畫. 65 UIViewAnimationOptionAutoreverse = 1 << 4, //!< 動畫運行到結束點後仍然以動畫方式回到初始點. 66 UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, //!< 忽略嵌套動畫時間設置. 67 UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, //!< 忽略嵌套動畫速度設置. 68 UIViewAnimationOptionAllowAnimatedContent = 1 << 7, //!< 動畫過程中重繪視圖(註意僅僅適用於轉場動畫). 69 UIViewAnimationOptionShowHideTransitionViews = 1 << 8, //!< 視圖切換時直接隱藏舊視圖、顯示新視圖,而不是將舊視圖從父視圖移除(僅僅適用於轉場動畫). 70 UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, //!< 不繼承父動畫設置或動畫類型. 71 72 UIViewAnimationOptionCurveEaseInOut = 0 << 16, //!< 動畫先緩慢,然後逐漸加速. 73 UIViewAnimationOptionCurveEaseIn = 1 << 16, //!< 動畫逐漸變慢. 74 UIViewAnimationOptionCurveEaseOut = 2 << 16, //!< 動畫逐漸加速. 75 UIViewAnimationOptionCurveLinear = 3 << 16, //!< 動畫勻速執行,預設值. 76 77 UIViewAnimationOptionTransitionNone = 0 << 20, //!< 沒有轉場動畫效果. 78 UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20, //!< 從左側翻轉效果. 79 UIViewAnimationOptionTransitionFlipFromRight = 2 << 20, //!< 從右側翻轉效果. 80 UIViewAnimationOptionTransitionCurlUp = 3 << 20, //!< 向後翻頁的動畫過渡效果. 81 UIViewAnimationOptionTransitionCurlDown = 4 << 20, //!< 向前翻頁的動畫過渡效果. 82 UIViewAnimationOptionTransitionCrossDissolve = 5 << 20, //!< 舊視圖溶解消失顯示下一個新視圖的效果. 83 UIViewAnimationOptionTransitionFlipFromTop = 6 << 20, //!< 從上方翻轉效果. 84 UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20, //!< 從底部翻轉效果. 85 86 UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24, //!< 預設的幀每秒. 87 UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24, //!< 60幀每秒的幀速率. 88 UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24, //!< 30幀每秒的幀速率. 89 90 } NS_ENUM_AVAILABLE_IOS(4_0); 91 92 typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) { 93 UIViewKeyframeAnimationOptionLayoutSubviews = UIViewAnimationOptionLayoutSubviews, //!< 動畫過程中保證子視圖跟隨運動. 94 UIViewKeyframeAnimationOptionAllowUserInteraction = UIViewAnimationOptionAllowUserInteraction, //!< 動畫過程中允許用戶交互. 95 UIViewKeyframeAnimationOptionBeginFromCurrentState = UIViewAnimationOptionBeginFromCurrentState, //!< 所有視圖從當前狀態開始運行. 96 UIViewKeyframeAnimationOptionRepeat = UIViewAnimationOptionRepeat, //!< 重覆運行動畫. 97 UIViewKeyframeAnimationOptionAutoreverse = UIViewAnimationOptionAutoreverse, //!< 動畫運行到結束點後仍然以動畫方式回到初始點. 98 UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, //!< 忽略嵌套動畫時間設置. 99 UIViewKeyframeAnimationOptionOverrideInheritedOptions = UIViewAnimationOptionOverrideInheritedOptions, //!< 不繼承父動畫設置或動畫類型. 100 101 UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, //!< 連續運算模式, 預設. 102 UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10, //!< 離散運算模式. 103 UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10, //!< 均勻執行運算模式. 104 UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10, //!< 平滑運算模式. 105 UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10 //!< 平滑均勻運算模式. 106 } NS_ENUM_AVAILABLE_IOS(7_0); 107 108 typedef NS_ENUM(NSUInteger, UISystemAnimation) { 109 UISystemAnimationDelete, //!< 系統刪除動畫 110 } NS_ENUM_AVAILABLE_IOS(7_0); 111 112 typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) { 113 UIViewTintAdjustmentModeAutomatic, //!< 自動的,與父視圖相同. 114 115 UIViewTintAdjustmentModeNormal, //!< 未經修改的. 116 UIViewTintAdjustmentModeDimmed, //!< 飽和、暗淡的原始色. 117 } NS_ENUM_AVAILABLE_IOS(7_0); 118 119 typedef NS_ENUM(NSInteger, UISemanticContentAttribute) { 120 UISemanticContentAttributeUnspecified = 0, //!< 未指定,預設值 121 UISemanticContentAttributePlayback, //!< 打開/ RW / FF等播放控制按鈕 122 UISemanticContentAttributeSpatial, //!< 控制導致某種形式的定向改變UI中,如分段控制文本對齊方式或在游戲中方向鍵 123 UISemanticContentAttributeForceLeftToRight, //!< 視圖總是從左向右佈局. 124 UISemanticContentAttributeForceRightToLeft //!< 視圖總是從右向左佈局. 125 } NS_ENUM_AVAILABLE_IOS(9_0); 126 127 @protocol UICoordinateSpace <NSObject> 128 129 /** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */ 130 - (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0); 131 /** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */ 132 - (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0); 133 /** 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect */ 134 - (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0); 135 /** 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect */ 136 - (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0); 137 138 /** 獲取bounds */ 139 @property (readonly, nonatomic) CGRect bounds NS_AVAILABLE_IOS(8_0); 140 141 @end 142 143 @class UIBezierPath, UIEvent, UIWindow, UIViewController, UIColor, UIGestureRecognizer, UIMotionEffect, CALayer, UILayoutGuide; 144 145 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate> 146 147 /** 返回主layer所使用的類 */ 148 #if UIKIT_DEFINE_AS_PROPERTIES 149 @property(class, nonatomic, readonly) Class layerClass; 150 #else 151 + (Class)layerClass; 152 #endif 153 154 /** 通過Frame初始化UI對象 */ 155 - (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER; 156 /** 用於xib初始化 */ 157 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER; 158 159 /** 設置用戶交互,預設YES允許用戶交互 */ 160 @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; 161 /** 控制項標記(父控制項可以通過tag找到對應的子控制項),預設為0 */ 162 @property(nonatomic) NSInteger tag; 163 /** 視圖圖層(可以用來設置圓角效果/陰影效果) */ 164 @property(nonatomic,readonly,strong) CALayer *layer; 165 166 /** 返回是否可以成為焦點, 預設NO */ 167 #if UIKIT_DEFINE_AS_PROPERTIES 168 @property(nonatomic,readonly) BOOL canBecomeFocused NS_AVAILABLE_IOS(9_0); 169 #else 170 - (BOOL)canBecomeFocused NS_AVAILABLE_IOS(9_0); 171 #endif 172 /** 是否可以被聚焦 */ 173 @property (readonly, nonatomic, getter=isFocused) BOOL focused NS_AVAILABLE_IOS(9_0); 174 175 /** 左右滑動翻轉效果 */ 176 @property (nonatomic) UISemanticContentAttribute semanticContentAttribute NS_AVAILABLE_IOS(9_0); 177 178 /** 獲取視圖的方向 */ 179 + (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute NS_AVAILABLE_IOS(9_0); 180 181 /** 獲取相對於指定視圖的界面方向 */ 182 + (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute relativeToLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection NS_AVAILABLE_IOS(10_0); 183 184 /** 返回即時內容的佈局的方向 */ 185 @property (readonly, nonatomic) UIUserInterfaceLayoutDirection effectiveUserInterfaceLayoutDirection NS_AVAILABLE_IOS(10_0); 186 187 @end 188 189 @interface UIView(UIViewGeometry) 190 191 /** 位置和尺寸(以父控制項的左上角為坐標原點(0, 0)) */ 192 @property(nonatomic) CGRect frame; 193 194 /** 位置和尺寸(以自己的左上角為坐標原點(0, 0)) */ 195 @property(nonatomic) CGRect bounds; 196 /** 中心點(以父控制項的左上角為坐標原點(0, 0)) */ 197 @property(nonatomic) CGPoint center; 198 /** 變形屬性(平移\縮放\旋轉) */ 199 @property(nonatomic) CGAffineTransform transform; 200 /** 視圖內容的縮放比例 */ 201 @property(nonatomic) CGFloat contentScaleFactor NS_AVAILABLE_IOS(4_0); 202 203 /** 是否支持多點觸摸,預設NO */ 204 @property(nonatomic,getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled __TVOS_PROHIBITED; 205 /** 是否獨占整個Touch事件,預設NO */ 206 @property(nonatomic,getter=isExclusiveTouch) BOOL exclusiveTouch __TVOS_PROHIBITED; 207 208 /** 在指定點上點擊測試指定事件 */ 209 - (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; 210 /** 判斷當前的點擊或者觸摸事件的點是否在當前的view中,預設返回YES */ 211 - (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; 212 213 /** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */ 214 - (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view; 215 /** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */ 216 - (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view; 217 /** 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect */ 218 - (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view; 219 /** 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect */ 220 - (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view; 221 222 /** 自動調整子視圖尺寸,預設YES則會根據autoresizingMask屬性自動調整子視圖尺寸 */ 223 @property(nonatomic) BOOL autoresizesSubviews; 224 /** 自動調整子視圖與父視圖的位置,預設UIViewAutoresizingNone */ 225 @property(nonatomic) UIViewAutoresizing autoresizingMask; 226 227 /** 返回“最佳”大小適合給定的大小 */ 228 - (CGSize)sizeThatFits:(CGSize)size; 229 /** 調整為剛好合適子視圖大小 */ 230 - (void)sizeToFit; 231 232 @end 233 234 @interface UIView(UIViewHierarchy) 235 236 /** 獲取父視圖 */ 237 @property(nullable, nonatomic,readonly) UIView *superview; 238 /** 獲取所有子視圖 */ 239 @property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *subviews; 240 /** 獲取視圖所在的Window */ 241 @property(nullable, nonatomic,readonly) UIWindow *window; 242 243 /** 從父視圖中移除控制項 */ 244 - (void)removeFromSuperview; 245 /** 插入子視圖(將子視圖插入到subviews數組中index這個位置) */ 246 - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; 247 /** 交換subviews數組中所存放子視圖的位置 */ 248 - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; 249 250 /** 添加子視圖(新添加的視圖在subviews數組的後面, 顯示在最上面) */ 251 - (void)addSubview:(UIView *)view; 252 /** 插入子視圖(將子視圖插到siblingSubview之下) */ 253 - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; 254 /** 插入子視圖(將子視圖插到siblingSubview之上) */ 255 - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; 256 257 /** 將子視圖拉到最上面來顯示 */ 258 - (void)bringSubviewToFront:(UIView *)view; 259 /** 將子視圖拉到最下麵來顯示 */ 260 - (void)sendSubviewToBack:(UIView *)view; 261 262 ##pragma mark - 系統自動調用(留給子類去實現) 263 /** 添加自視圖完成後調用 */ 264 - (void)didAddSubview:(UIView *)subview; 265 /** 將要移除自視圖時調用 */ 266 - (void)willRemoveSubview:(UIView *)subview; 267 268 /** 將要移動到新父視圖時調用 */ 269 - (void)willMoveToSuperview:(nullable UIView *)newSuperview; 270 /** 移動到新父視圖完成後調用 */ 271 - (void)didMoveToSuperview; 272 /** 將要移動到新Window時調用 */ 273 - (void)willMoveToWindow:(nullable UIWindow *)newWindow; 274 /** 移動到新Window完成後調用 */ 275 - (void)didMoveToWindow; 276 277 /** 判斷view是否為子類 */ 278 - (BOOL)isDescendantOfView:(UIView *)view; 279 /** 通過tag獲得對應的子視圖 */ 280 - (nullable __kindof UIView *)viewWithTag:(NSInteger)tag; 281 282 /** 對現在有佈局有調整更改後,使用這個方法進行更新 */ 283 - (void)setNeedsLayout; 284 /** 強制進行更新layout */ 285 - (void)layoutIfNeeded; 286 287 /** 控制項的frame發生改變的時候就會調用,一般在這裡重寫佈局子控制項的位置和尺寸 */ 288 - (void)layoutSubviews; 289 290 /** 設置view之間的間距,該屬性只對autolayout佈局有效 */ 291 @property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0); 292 /** 是否將當前視圖的間距和父視圖相同,預設是NO */ 293 @property (nonatomic) BOOL preservesSuperviewLayoutMargins NS_AVAILABLE_IOS(8_0); 294 /** 改變view的layoutMargins這個屬性時,會觸發這個方法 */ 295 - (void)layoutMarginsDidChange NS_AVAILABLE_IOS(8_0); 296 297 /** 視圖間距引導 */ 298 @property(readonly,strong) UILayoutGuide *layoutMarginsGuide NS_AVAILABLE_IOS(9_0); 299 300 /** 獲取此區域的內的佈局引導 */ 301 @property (nonatomic, readonly, strong) UILayoutGuide *readableContentGuide NS_AVAILABLE_IOS(9_0); 302 @end 303 304 @interface UIView(UIViewRendering) 305 306 /** 重寫drawRect方法,在可以這裡進行繪圖操作。*/ 307 - (void)drawRect:(CGRect)rect; 308 309 /** 標記整個視圖的邊界矩形需要重繪, 調用這個方法會自動調用drawRect方法 */ 310 - (void)setNeedsDisplay; 311 /** 標記在指定區域內的視圖的邊界需要重繪, 調用這個方法會自動調用drawRect方法 */ 312 - (void)setNeedsDisplayInRect:(CGRect)rect; 313 314 /** 是否裁剪超出Bounds範圍的子控制項,預設NO */ 315 @property(nonatomic) BOOL clipsToBounds; 316 /** 設置背景顏色,預設nil */ 317 @property(nullable, nonatomic,copy) UIColor *backgroundColor UI_APPEARANCE_SELECTOR; 318 /** 設置透明度(範圍0.0~1.0),預設1.0 */ 319 @property(nonatomic) CGFloat alpha; 320 /** 設置是否不透明,預設YES不透明 */ 321 @property(nonatomic,getter=isOpaque) BOOL opaque; 322 /** 視圖重繪前是否先清理以前的內容,預設YES */ 323 @property(nonatomic) BOOL clearsContextBeforeDrawing; 324 /** 設置是否隱藏,預設NO不隱藏 */ 325 @property(nonatomic,getter=isHidden) BOOL hidden; 326 /** 內容顯示的模式,預設UIViewContentModeScaleToFill */ 327 @property(nonatomic) UIViewContentMode contentMode; 328 /** 拉伸屬性,如圖片拉伸 */ 329 @property(nonatomic) CGRect contentStretch NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED; 330 331 /** 蒙板view */ 332 @property(nullable, nonatomic,strong) UIView *maskView NS_AVAILABLE_IOS(8_0); 333 334 /** 改變應用程式的外觀的顏色。預設為nil */ 335 @property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0); 336 337 /** 可以使tintColor變暗,因此整個視圖層次變暗 */ 338 @property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0); 339 340 /** 覆蓋這個方法的目的是為了當tintColor改變的時候自定義一些行為 */ 341 - (void)tintColorDidChange NS_AVAILABLE_IOS(7_0); 342 343 @end 344 345 @interface UIView(UIViewAnimation) 346 347 /** 開始動畫 */ 348 + (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context; 349 /** 提交動畫 */ 350 + (void)commitAnimations; 351 352 /** 設置動畫代理, 預設nil */ 353 + (void)setAnimationDelegate:(nullable id)delegate