ios開發 之 UIView詳解

ONNULL


UIView詳解

//
//  UIView.h
//  ZMHeaderFile
//
//  Created by ZengZhiming on 2017/5/22.
//  Copyright © 2017年 菜鳥基地. All rights reserved.
//
//  詳解 UIResponder.h
//  Version iOS 10.3
//

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIResponder.h>
#import <UIKit/UIInterface.h>
#import <UIKit/UIKitDefines.h>
#import <UIKit/UIAppearance.h>
#import <UIKit/UIDynamicBehavior.h>
#import <UIKit/NSLayoutConstraint.h>
#import <UIKit/UITraitCollection.h>
#import <UIKit/UIFocus.h>

NS_ASSUME_NONNULL_BEGIN

/** 動畫的曲線枚舉 */
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    UIViewAnimationCurveEaseInOut,  //!< 慢進慢出(默認值).
    UIViewAnimationCurveEaseIn,     //!< 慢進.
    UIViewAnimationCurveEaseOut,    //!< 慢出.
    UIViewAnimationCurveLinear,     //!< 勻速.
};

//!< UIView內容填充模式.
typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill,       //!< 縮放內容到合適比例大小.
    UIViewContentModeScaleAspectFit,    //!< 縮放內容到合適的大小,邊界多餘部分透明.
    UIViewContentModeScaleAspectFill,   //!< 縮放內容填充到指定大小,邊界多餘的部分省略.
    UIViewContentModeRedraw,            //!< 重繪視圖邊界 (需調用 -setNeedsDisplay).
    UIViewContentModeCenter,            //!< 視圖保持等比縮放.
    UIViewContentModeTop,               //!< 視圖頂部對齊.
    UIViewContentModeBottom,            //!< 視圖底部對齊.
    UIViewContentModeLeft,              //!< 視圖左側對齊.
    UIViewContentModeRight,             //!< 視圖右側對齊.
    UIViewContentModeTopLeft,           //!< 視圖左上角對齊.
    UIViewContentModeTopRight,          //!< 視圖右上角對齊.
    UIViewContentModeBottomLeft,        //!< 視圖左下角對齊.
    UIViewContentModeBottomRight,       //!< 視圖右下角對齊.
};

/** UIView動畫過渡效果 */
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,          //!< 無效果.
    UIViewAnimationTransitionFlipFromLeft,  //!< 沿視圖垂直中心軸左到右移動.
    UIViewAnimationTransitionFlipFromRight, //!< 沿視圖垂直中心軸右到左移動.
    UIViewAnimationTransitionCurlUp,        //!< 由底部向上捲起.
    UIViewAnimationTransitionCurlDown,      //!< 由頂部向下展開.
};

/** 自動調整大小方式 */
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,     //!< 不自動調整.
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,//!< 自動調整與superView左邊的距離,保證與superView右邊的距離不變.
    UIViewAutoresizingFlexibleWidth        = 1 << 1,//!< 自動調整自己的寬度,保證與superView左邊和右邊的距離不變.
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,//!< 自動調整與superView的右邊距離,保證與superView左邊的距離不變.
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,//!< 自動調整與superView頂部的距離,保證與superView底部的距離不變.
    UIViewAutoresizingFlexibleHeight       = 1 << 4,//!< 自動調整自己的高度,保證與superView頂部和底部的距離不變.
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5 //!< 自動調整與superView底部的距離,也就是說,與superView頂部的距離不變.
};

/** UIView動畫選項 */
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
    UIViewAnimationOptionLayoutSubviews            = 1 <<  0, //!< 動畫過程中保證子視圖跟隨運動.
    UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, //!< 動畫過程中允許用戶交互.
    UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, //!< 所有視圖從當前狀態開始運行.
    UIViewAnimationOptionRepeat                    = 1 <<  3, //!< 重複運行動畫.
    UIViewAnimationOptionAutoreverse               = 1 <<  4, //!< 動畫運行到結束點後仍然以動畫方式回到初始點.
    UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, //!< 忽略嵌套動畫時間設置.
    UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, //!< 忽略嵌套動畫速度設置.
    UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, //!< 動畫過程中重繪視圖(注意僅僅適用於轉場動畫).
    UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, //!< 視圖切換時直接隱藏舊視圖、顯示新視圖,而不是將舊視圖從父視圖移除(僅僅適用於轉場動畫).
    UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, //!< 不繼承父動畫設置或動畫類型.
    
    UIViewAnimationOptionCurveEaseInOut            = 0 << 16, //!< 動畫先緩慢,然後逐漸加速.
    UIViewAnimationOptionCurveEaseIn               = 1 << 16, //!< 動畫逐漸變慢.
    UIViewAnimationOptionCurveEaseOut              = 2 << 16, //!< 動畫逐漸加速.
    UIViewAnimationOptionCurveLinear               = 3 << 16, //!< 動畫勻速執行,默認值.
    
    UIViewAnimationOptionTransitionNone            = 0 << 20, //!< 沒有轉場動畫效果.
    UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20, //!< 從左側翻轉效果.
    UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20, //!< 從右側翻轉效果.
    UIViewAnimationOptionTransitionCurlUp          = 3 << 20, //!< 向後翻頁的動畫過渡效果.
    UIViewAnimationOptionTransitionCurlDown        = 4 << 20, //!< 向前翻頁的動畫過渡效果.
    UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20, //!< 舊視圖溶解消失顯示下一個新視圖的效果.
    UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20, //!< 從上方翻轉效果.
    UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20, //!< 從底部翻轉效果.
    
    UIViewAnimationOptionPreferredFramesPerSecondDefault     = 0 << 24, //!< 默認的幀每秒.
    UIViewAnimationOptionPreferredFramesPerSecond60          = 3 << 24, //!< 60幀每秒的幀速率.
    UIViewAnimationOptionPreferredFramesPerSecond30          = 7 << 24, //!< 30幀每秒的幀速率.
    
} NS_ENUM_AVAILABLE_IOS(4_0);

typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) {
    UIViewKeyframeAnimationOptionLayoutSubviews            = UIViewAnimationOptionLayoutSubviews, //!< 動畫過程中保證子視圖跟隨運動.
    UIViewKeyframeAnimationOptionAllowUserInteraction      = UIViewAnimationOptionAllowUserInteraction, //!< 動畫過程中允許用戶交互.
    UIViewKeyframeAnimationOptionBeginFromCurrentState     = UIViewAnimationOptionBeginFromCurrentState, //!< 所有視圖從當前狀態開始運行.
    UIViewKeyframeAnimationOptionRepeat                    = UIViewAnimationOptionRepeat, //!< 重複運行動畫.
    UIViewKeyframeAnimationOptionAutoreverse               = UIViewAnimationOptionAutoreverse, //!< 動畫運行到結束點後仍然以動畫方式回到初始點.
    UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, //!< 忽略嵌套動畫時間設置.
    UIViewKeyframeAnimationOptionOverrideInheritedOptions  = UIViewAnimationOptionOverrideInheritedOptions, //!< 不繼承父動畫設置或動畫類型.
    
    UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10, //!< 連續運算模式, 默認.
    UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 10, //!< 離散運算模式.
    UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 10, //!< 均勻執行運算模式.
    UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 10, //!< 平滑運算模式.
    UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10  //!< 平滑均勻運算模式.
} NS_ENUM_AVAILABLE_IOS(7_0);

typedef NS_ENUM(NSUInteger, UISystemAnimation) {
    UISystemAnimationDelete,    //!< 系統刪除動畫
} NS_ENUM_AVAILABLE_IOS(7_0);

typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
    UIViewTintAdjustmentModeAutomatic,  //!< 自動的,與父視圖相同.
    
    UIViewTintAdjustmentModeNormal,     //!< 未經修改的.
    UIViewTintAdjustmentModeDimmed,     //!< 飽和、暗淡的原始色.
} NS_ENUM_AVAILABLE_IOS(7_0);

typedef NS_ENUM(NSInteger, UISemanticContentAttribute) {
    UISemanticContentAttributeUnspecified = 0,  //!< 未指定,默認值
    UISemanticContentAttributePlayback,         //!< 打開/ RW / FF等播放控制按鈕
    UISemanticContentAttributeSpatial,          //!< 控制導致某種形式的定向改變UI中,如分段控制文本對齊方式或在遊戲中方向鍵
    UISemanticContentAttributeForceLeftToRight, //!< 視圖總是從左向右佈局.
    UISemanticContentAttributeForceRightToLeft  //!< 視圖總是從右向左佈局.
} NS_ENUM_AVAILABLE_IOS(9_0);

@protocol UICoordinateSpace <NSObject>

/** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect */
- (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect */
- (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace NS_AVAILABLE_IOS(8_0);

/** 獲取bounds */
@property (readonly, nonatomic) CGRect bounds NS_AVAILABLE_IOS(8_0);

@end

@class UIBezierPath, UIEvent, UIWindow, UIViewController, UIColor, UIGestureRecognizer, UIMotionEffect, CALayer, UILayoutGuide;

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusItem, CALayerDelegate>

/** 返回主layer所使用的類 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) Class layerClass;
#else
+ (Class)layerClass;
#endif

/** 通過Frame初始化UI對象 */
- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
/** 用於xib初始化 */
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

/** 設置用戶交互,默認YES允許用戶交互 */
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
/** 控件標記(父控件可以通過tag找到對應的子控件),默認爲0 */
@property(nonatomic)                                 NSInteger tag;
/** 視圖圖層(可以用來設置圓角效果/陰影效果) */
@property(nonatomic,readonly,strong)                 CALayer  *layer;

/** 返回是否可以成爲焦點, 默認NO */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic,readonly) BOOL canBecomeFocused NS_AVAILABLE_IOS(9_0);
#else
- (BOOL)canBecomeFocused NS_AVAILABLE_IOS(9_0);
#endif
/** 是否可以被聚焦 */
@property (readonly, nonatomic, getter=isFocused) BOOL focused NS_AVAILABLE_IOS(9_0);

/** 左右滑動翻轉效果 */
@property (nonatomic) UISemanticContentAttribute semanticContentAttribute NS_AVAILABLE_IOS(9_0);

/** 獲取視圖的方向 */
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute NS_AVAILABLE_IOS(9_0);

/** 獲取相對於指定視圖的界面方向 */
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute relativeToLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection NS_AVAILABLE_IOS(10_0);

/** 返回即時內容的佈局的方向 */
@property (readonly, nonatomic) UIUserInterfaceLayoutDirection effectiveUserInterfaceLayoutDirection NS_AVAILABLE_IOS(10_0);

@end

@interface UIView(UIViewGeometry)

/** 位置和尺寸(以父控件的左上角爲座標原點(0, 0)) */
@property(nonatomic) CGRect            frame;

/** 位置和尺寸(以自己的左上角爲座標原點(0, 0)) */
@property(nonatomic) CGRect            bounds;
/** 中心點(以父控件的左上角爲座標原點(0, 0)) */
@property(nonatomic) CGPoint           center;
/** 變形屬性(平移\縮放\旋轉) */
@property(nonatomic) CGAffineTransform transform;
/** 視圖內容的縮放比例 */
@property(nonatomic) CGFloat           contentScaleFactor NS_AVAILABLE_IOS(4_0);

/** 是否支持多點觸摸,默認NO */
@property(nonatomic,getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled __TVOS_PROHIBITED;
/** 是否獨佔整個Touch事件,默認NO */
@property(nonatomic,getter=isExclusiveTouch) BOOL       exclusiveTouch __TVOS_PROHIBITED;

/** 在指定點上點擊測試指定事件 */
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event;
/** 判斷當前的點擊或者觸摸事件的點是否在當前的view中,默認返回YES */
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;

/** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
/** 將像素point由point所在視圖轉換到目標視圖view中,返回在目標視圖view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
/** 將rect由rect所在視圖轉換到目標視圖view中,返回在目標視圖view中的rect */
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
/** 將rect從view中轉換到當前視圖中,返回在當前視圖中的rect */
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

/** 自動調整子視圖尺寸,默認YES則會根據autoresizingMask屬性自動調整子視圖尺寸 */
@property(nonatomic) BOOL               autoresizesSubviews;
/** 自動調整子視圖與父視圖的位置,默認UIViewAutoresizingNone */
@property(nonatomic) UIViewAutoresizing autoresizingMask;

/** 返回“最佳”大小適合給定的大小 */
- (CGSize)sizeThatFits:(CGSize)size;
/** 調整爲剛好合適子視圖大小 */
- (void)sizeToFit;

@end

@interface UIView(UIViewHierarchy)

/** 獲取父視圖 */
@property(nullable, nonatomic,readonly) UIView       *superview;
/** 獲取所有子視圖 */
@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *subviews;
/** 獲取視圖所在的Window */
@property(nullable, nonatomic,readonly) UIWindow     *window;

/** 從父視圖中移除控件 */
- (void)removeFromSuperview;
/** 插入子視圖(將子視圖插入到subviews數組中index這個位置) */
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
/** 交換subviews數組中所存放子視圖的位置 */
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

/** 添加子視圖(新添加的視圖在subviews數組的後面, 顯示在最上面) */
- (void)addSubview:(UIView *)view;
/** 插入子視圖(將子視圖插到siblingSubview之下) */
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
/** 插入子視圖(將子視圖插到siblingSubview之上) */
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

/** 將子視圖拉到最上面來顯示 */
- (void)bringSubviewToFront:(UIView *)view;
/** 將子視圖拉到最下面來顯示 */
- (void)sendSubviewToBack:(UIView *)view;

##pragma mark - 系統自動調用(留給子類去實現)
/** 添加自視圖完成後調用 */
- (void)didAddSubview:(UIView *)subview;
/** 將要移除自視圖時調用 */
- (void)willRemoveSubview:(UIView *)subview;

/** 將要移動到新父視圖時調用 */
- (void)willMoveToSuperview:(nullable UIView *)newSuperview;
/** 移動到新父視圖完成後調用 */
- (void)didMoveToSuperview;
/** 將要移動到新Window時調用 */
- (void)willMoveToWindow:(nullable UIWindow *)newWindow;
/** 移動到新Window完成後調用 */
- (void)didMoveToWindow;

/** 判斷view是否爲子類 */
- (BOOL)isDescendantOfView:(UIView *)view;
/** 通過tag獲得對應的子視圖 */
- (nullable __kindof UIView *)viewWithTag:(NSInteger)tag;

/** 對現在有佈局有調整更改後,使用這個方法進行更新 */
- (void)setNeedsLayout;
/** 強制進行更新layout */
- (void)layoutIfNeeded;

/** 控件的frame發生改變的時候就會調用,一般在這裏重寫佈局子控件的位置和尺寸 */
- (void)layoutSubviews;

/** 設置view之間的間距,該屬性只對autolayout佈局有效 */
@property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0);
/** 是否將當前視圖的間距和父視圖相同,默認是NO */
@property (nonatomic) BOOL preservesSuperviewLayoutMargins NS_AVAILABLE_IOS(8_0);
/** 改變view的layoutMargins這個屬性時,會觸發這個方法 */
- (void)layoutMarginsDidChange NS_AVAILABLE_IOS(8_0);

/** 視圖間距引導 */
@property(readonly,strong) UILayoutGuide *layoutMarginsGuide NS_AVAILABLE_IOS(9_0);

/** 獲取此區域的內的佈局引導 */
@property (nonatomic, readonly, strong) UILayoutGuide *readableContentGuide  NS_AVAILABLE_IOS(9_0);
@end

@interface UIView(UIViewRendering)

/** 重寫drawRect方法,在可以這裏進行繪圖操作。*/
- (void)drawRect:(CGRect)rect;

/** 標記整個視圖的邊界矩形需要重繪, 調用這個方法會自動調用drawRect方法 */
- (void)setNeedsDisplay;
/** 標記在指定區域內的視圖的邊界需要重繪, 調用這個方法會自動調用drawRect方法 */
- (void)setNeedsDisplayInRect:(CGRect)rect;

/** 是否裁剪超出Bounds範圍的子控件,默認NO */
@property(nonatomic)                 BOOL              clipsToBounds;
/** 設置背景顏色,默認nil */
@property(nullable, nonatomic,copy)  UIColor          *backgroundColor UI_APPEARANCE_SELECTOR;
/** 設置透明度(範圍0.0~1.0),默認1.0 */
@property(nonatomic)                 CGFloat           alpha;
/** 設置是否不透明,默認YES不透明 */
@property(nonatomic,getter=isOpaque) BOOL              opaque;
/** 視圖重繪前是否先清理以前的內容,默認YES */
@property(nonatomic)                 BOOL              clearsContextBeforeDrawing;
/** 設置是否隱藏,默認NO不隱藏 */
@property(nonatomic,getter=isHidden) BOOL              hidden;
/** 內容顯示的模式,默認UIViewContentModeScaleToFill */
@property(nonatomic)                 UIViewContentMode contentMode;
/** 拉伸屬性,如圖片拉伸 */
@property(nonatomic)                 CGRect            contentStretch NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED;

/** 蒙板view */
@property(nullable, nonatomic,strong)          UIView           *maskView NS_AVAILABLE_IOS(8_0);

/** 改變應用程序的外觀的顏色。默認爲nil */
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);

/** 可以使tintColor變暗,因此整個視圖層次變暗 */
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);

/** 覆蓋這個方法的目的是爲了當tintColor改變的時候自定義一些行爲 */
- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);

@end

@interface UIView(UIViewAnimation)

/** 開始動畫 */
+ (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context;
/** 提交動畫 */
+ (void)commitAnimations;

/** 設置動畫代理, 默認nil */
+ (void)setAnimationDelegate:(nullable id)delegate;
/** 動畫將要開始時執行方法(必須要先設置動畫代理), 默認NULL */
+ (void)setAnimationWillStartSelector:(nullable SEL)selector;
/** 動畫已結束時執行方法(必須要先設置動畫代理), 默認NULL */
+ (void)setAnimationDidStopSelector:(nullable SEL)selector;
/** 設置動畫時長, 默認0.2秒 */
+ (void)setAnimationDuration:(NSTimeInterval)duration;
/** 動畫延遲執行時間, 默認0.0秒 */
+ (void)setAnimationDelay:(NSTimeInterval)delay;
/** 設置在動畫塊內部動畫屬性改變的開始時間, 默認now ([NSDate date]) */
+ (void)setAnimationStartDate:(NSDate *)startDate;
/** 設置動畫曲線, 默認UIViewAnimationCurveEaseInOut */
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;
/** 動畫的重複播放次數, 默認0 */
+ (void)setAnimationRepeatCount:(float)repeatCount;
/** 設置是否自定翻轉當前的動畫效果, 默認NO */
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
/** 設置動畫從當前狀態開始播放, 默認NO */
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;

/** 在動畫塊中爲視圖設置過渡動畫 */
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;

/** 設置是否激活動畫 */
+ (void)setAnimationsEnabled:(BOOL)enabled;
/** 返回一個布爾值表示動畫是否結束 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) BOOL areAnimationsEnabled;
#else
+ (BOOL)areAnimationsEnabled;
#endif
/** 先檢查動畫當前是否啓用,然後禁止動畫,執行block內的方法,最後重新啓用動畫,而且這個方法不會阻塞基於CoreAnimation的動畫 */
+ (void)performWithoutAnimation:(void (NS_NOESCAPE ^)(void))actionsWithoutAnimation NS_AVAILABLE_IOS(7_0);

/** 當前動畫的持續時間 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) NSTimeInterval inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);
#else
+ (NSTimeInterval)inheritedAnimationDuration NS_AVAILABLE_IOS(9_0);
#endif

@end

@interface UIView(UIViewAnimationWithBlocks)

/** 用於對一個或多個視圖的改變的持續時間、延時、選項動畫完成時的操作 */
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

/** 用於對一個或多個視圖的改變的持續時間、選項動畫完成時的操作,默認:delay = 0.0, options = 0 */
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

/** 用於對一個或多個視圖的改變的持續時間內動畫完成時的操作,默認:delay = 0.0, options = 0, completion = NULL */
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);

/** 使用與物理彈簧運動相對應的定時曲線執行視圖動畫 */
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

/** 爲指定的容器視圖創建轉換動畫 */
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

/** 使用給定的參數在指定視圖之間創建轉換動畫 */
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview

/** 在一個或多個視圖上執行指定的系統提供的動畫,以及定義的可選並行動畫 */
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray<__kindof UIView *> *)views options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

@end

/** UIView的關鍵幀動畫 */
@interface UIView (UIViewKeyframeAnimations)

/** 創建一個動畫塊對象,可用於爲當前視圖設置基於關鍵幀的動畫 */
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
/** 添加指定開始時間、持續時間的關鍵幀動畫(起始和持續時間是0.0和1.0之間的值) */
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime relativeDuration:(double)frameDuration animations:(void (^)(void))animations NS_AVAILABLE_IOS(7_0);

@end

@interface UIView (UIViewGestureRecognizers)

/** 當前視圖所附加的所有手勢識別器 */
@property(nullable, nonatomic,copy) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2);

/** 添加一個手勢識別器 */
- (void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer NS_AVAILABLE_IOS(3_2);
/** 移除一個手勢識別器 */
- (void)removeGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer NS_AVAILABLE_IOS(3_2);

/** 開始一個手勢識別器 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer NS_AVAILABLE_IOS(6_0);

@end

@interface UIView (UIViewMotionEffects)

/** 添加運動效果,當傾斜設備時視圖稍微改變其位置 */
- (void)addMotionEffect:(UIMotionEffect *)effect NS_AVAILABLE_IOS(7_0);

/** 移除運動效果 */
- (void)removeMotionEffect:(UIMotionEffect *)effect NS_AVAILABLE_IOS(7_0);

/** 所有添加的運動效果 */
@property (copy, nonatomic) NSArray<__kindof UIMotionEffect *> *motionEffects NS_AVAILABLE_IOS(7_0);

@end


typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
    UILayoutConstraintAxisHorizontal = 0,   //!< 水平約束.
    UILayoutConstraintAxisVertical = 1      //!< 豎直約束.
};


@interface UIView (UIConstraintBasedLayoutInstallingConstraints)

/** 獲取所有約束 */
@property(nonatomic,readonly) NSArray<__kindof NSLayoutConstraint *> *constraints NS_AVAILABLE_IOS(6_0);

/** 添加一個約束 */
- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);
/** 添加多個約束 */
- (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0);
/** 移除一個約束 */
- (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);
/** 移除多個約束 */
- (void)removeConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0);
@end


@interface UIView (UIConstraintBasedLayoutCoreMethods)
/** 更新視圖和其子視圖的約束 */
- (void)updateConstraintsIfNeeded NS_AVAILABLE_IOS(6_0);
/** 爲視圖更新約束,可以重寫這個方法來設置當前view局部的佈局約束 */
- (void)updateConstraints NS_AVAILABLE_IOS(6_0) NS_REQUIRES_SUPER;
/** 視圖的約束是否需要更新 */
- (BOOL)needsUpdateConstraints NS_AVAILABLE_IOS(6_0);
/** 設置視圖的約束需要更新,調用這個方法,系統會調用updateConstraints去更新佈局 */
- (void)setNeedsUpdateConstraints NS_AVAILABLE_IOS(6_0);
@end


@interface UIView (UIConstraintBasedCompatibility)

/** 是否啓用自動佈局約束,默認YES. IB默認是NO */
@property(nonatomic) BOOL translatesAutoresizingMaskIntoConstraints NS_AVAILABLE_IOS(6_0);

/** 是否使用約束佈局 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) BOOL requiresConstraintBasedLayout NS_AVAILABLE_IOS(6_0);
#else
+ (BOOL)requiresConstraintBasedLayout NS_AVAILABLE_IOS(6_0);
#endif

@end


@interface UIView (UIConstraintBasedLayoutLayering)

/** 返回給定框架的視圖的對齊矩陣 */
- (CGRect)alignmentRectForFrame:(CGRect)frame NS_AVAILABLE_IOS(6_0);
/** 返回給定對齊矩形的視圖的frame */
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect NS_AVAILABLE_IOS(6_0);

/** 返回從視圖的frame上定義的對齊矩陣的邊框 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) UIEdgeInsets alignmentRectInsets NS_AVAILABLE_IOS(6_0);
#else
- (UIEdgeInsets)alignmentRectInsets NS_AVAILABLE_IOS(6_0);
#endif

/** 返回滿足基線約束條件的視圖 */
- (UIView *)viewForBaselineLayout NS_DEPRECATED_IOS(6_0, 9_0, "Override -viewForFirstBaselineLayout or -viewForLastBaselineLayout as appropriate, instead") __TVOS_PROHIBITED;

/** 返回用於滿足第一基線約束的視圖 */
@property(readonly,strong) UIView *viewForFirstBaselineLayout NS_AVAILABLE_IOS(9_0);

/** 返回用於滿足上次基線約束的視圖 */
@property(readonly,strong) UIView *viewForLastBaselineLayout NS_AVAILABLE_IOS(9_0);


UIKIT_EXTERN const CGFloat UIViewNoIntrinsicMetric NS_AVAILABLE_IOS(6_0); // -1
/** 返回接收對象的原本大小 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) CGSize intrinsicContentSize NS_AVAILABLE_IOS(6_0);
#else
- (CGSize)intrinsicContentSize NS_AVAILABLE_IOS(6_0);
#endif
/** 廢除視圖原本內容的size */
- (void)invalidateIntrinsicContentSize NS_AVAILABLE_IOS(6_0);

/** 設置當視圖要變大時,視圖的壓縮改變方式,返回一個優先權(確定view有多大的優先級阻止自己變大) */
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
/** 設置放先權 */
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

/** 設置當視圖要變小時,視圖的壓縮改變方式,是水平縮小還是垂直縮小,並返回一個優先權(確定有多大的優先級阻止自己變小) */
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
/** 設置優先權 */
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
@end

// Size To Fit

UIKIT_EXTERN const CGSize UILayoutFittingCompressedSize NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN const CGSize UILayoutFittingExpandedSize NS_AVAILABLE_IOS(6_0);

@interface UIView (UIConstraintBasedLayoutFittingSize)
/** 返回滿足持有約束的視圖的size */
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize NS_AVAILABLE_IOS(6_0);
/** 返回滿足它所包含的約束的視圖的大小 */
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority NS_AVAILABLE_IOS(8_0);
@end

@interface UIView (UILayoutGuideSupport)

/** 此視圖擁有佈局嚮導對象的數組 */
@property(nonatomic,readonly,copy) NSArray<__kindof UILayoutGuide *> *layoutGuides NS_AVAILABLE_IOS(9_0);

/** 向視圖中添加布局嚮導 */
- (void)addLayoutGuide:(UILayoutGuide *)layoutGuide NS_AVAILABLE_IOS(9_0);

/** 向視圖中添加布局嚮導 */
- (void)removeLayoutGuide:(UILayoutGuide *)layoutGuide NS_AVAILABLE_IOS(9_0);
@end

@class NSLayoutXAxisAnchor,NSLayoutYAxisAnchor,NSLayoutDimension;
@interface UIView (UIViewLayoutConstraintCreation)
/** 佈局視圖的前緣框的佈局錨點 */
@property(readonly, strong) NSLayoutXAxisAnchor *leadingAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的後緣邊框的佈局錨點 */
@property(readonly, strong) NSLayoutXAxisAnchor *trailingAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的左邊框的佈局錨點 */
@property(readonly, strong) NSLayoutXAxisAnchor *leftAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的右邊框的佈局錨點 */
@property(readonly, strong) NSLayoutXAxisAnchor *rightAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的頂邊框的佈局錨點 */
@property(readonly, strong) NSLayoutYAxisAnchor *topAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的底邊框的佈局錨點 */
@property(readonly, strong) NSLayoutYAxisAnchor *bottomAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的寬度 */
@property(readonly, strong) NSLayoutDimension *widthAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的高度 */
@property(readonly, strong) NSLayoutDimension *heightAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的水平中心軸 */
@property(readonly, strong) NSLayoutXAxisAnchor *centerXAnchor NS_AVAILABLE_IOS(9_0);
/** 佈局視圖的垂直中心軸 */
@property(readonly, strong) NSLayoutYAxisAnchor *centerYAnchor NS_AVAILABLE_IOS(9_0);
/** 一個代表對視圖中的文本的最高線基線佈置錨 */
@property(readonly, strong) NSLayoutYAxisAnchor *firstBaselineAnchor NS_AVAILABLE_IOS(9_0);
/** 一個代表對視圖中的文本的最低線基線佈置錨 */
@property(readonly, strong) NSLayoutYAxisAnchor *lastBaselineAnchor NS_AVAILABLE_IOS(9_0);

@end

@interface UIView (UIConstraintBasedLayoutDebugging)

/** 獲得實體在不同方向上所有的佈局約束 */
- (NSArray<__kindof NSLayoutConstraint *> *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

/** 可以知道當前視圖的佈局是否會有歧義 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL hasAmbiguousLayout NS_AVAILABLE_IOS(6_0);
#else
- (BOOL)hasAmbiguousLayout NS_AVAILABLE_IOS(6_0);
#endif

/** 這個方法會隨機改變視圖的layout到另外一個有效的layout。這樣我們就可以很清楚的看到哪一個layout導致了整體的佈局約束出現了錯誤,或者我們應該增加更多的佈局約束 */
- (void)exerciseAmbiguityInLayout NS_AVAILABLE_IOS(6_0);
@end

/** 約束調試,只在DEBUG環境下被調用 */
@interface UILayoutGuide (UIConstraintBasedLayoutDebugging)

/** 獲得實體在不同方向上所有的佈局約束 */
- (NSArray<__kindof NSLayoutConstraint *> *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(10_0);

/** 可以知道當前視圖的佈局是否會有歧義 */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly) BOOL hasAmbiguousLayout NS_AVAILABLE_IOS(10_0);
#else
- (BOOL)hasAmbiguousLayout NS_AVAILABLE_IOS(10_0);
#endif
@end

#pragma mark - View狀態保存恢復
@interface UIView (UIStateRestoration)
/** 標示是否支持保存,恢復視圖狀態信息 */
@property (nullable, nonatomic, copy) NSString *restorationIdentifier NS_AVAILABLE_IOS(6_0);
/** 保存視圖狀態相關的信息 */
- (void) encodeRestorableStateWithCoder:(NSCoder *)coder NS_AVAILABLE_IOS(6_0);
/** 恢復和保持視圖狀態相關信息 */
- (void) decodeRestorableStateWithCoder:(NSCoder *)coder NS_AVAILABLE_IOS(6_0);
@end

#pragma mark - View快照
@interface UIView (UISnapshotting)
/** 將當前顯示的view截取成一個新的view */
- (nullable UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
/** 縮放一個view默認是從中心點進行縮放的 */
- (nullable UIView *)resizableSnapshotViewFromRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates withCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(7_0);
/** 屏幕快照 */
- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
@end

NS_ASSUME_NONNULL_END



ONNULL





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章