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