UIView 半解(1)

原文:cyrill's blog

//
//  UIView.h
//  UIKit
//
//  Copyright (c) 2005-2017 Apple Inc. All rights reserved.
//

// 基础框架入口
#import 
#import 
#import 
#import 
#import 
#import 
#import 
#import 
#import 
#import 

// ------------------------------------------------------------------------------------
// UIViewAnimationCurve设置动画块中的动画属性变化的曲线
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
    // slow at beginning and end  缓入缓出,中间快
    UIViewAnimationCurveEaseInOut,
    // slow at beginning 由慢到特别快(缓入快出)
    UIViewAnimationCurveEaseIn,
    // slow at end 由快到慢(快入缓出)
    UIViewAnimationCurveEaseOut,
    // 动画匀速执行,默认值。
    UIViewAnimationCurveLinear
};

// ------------------------------------------------------------------------------------
typedef NS_ENUM(NSInteger, UIViewContentMode) {
    // 伸缩至高度及宽度与UIView的大小一致,默认值
    UIViewContentModeScaleToFill,
    // contents scaled to fit with fixed aspect. remainder is transparent
    // 在不超出UIView尺寸范围内,且不改变比例的情况下伸缩至最大尺寸,边界多余部分透明
    UIViewContentModeScaleAspectFit,
    // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    // 超出UIView的高度或者宽度,伸缩至不改变比例的最大尺寸,边界多余的部分可能会被剪切掉
    UIViewContentModeScaleAspectFill,
    // redraw on bounds change (calls -setNeedsDisplay) 重绘视图边界
    UIViewContentModeRedraw,
    // contents remain same size. positioned adjusted. 视图保持等比缩放
    UIViewContentModeCenter,
    UIViewContentModeTop, // 视图顶部对齐
    UIViewContentModeBottom,// 视图底部对齐
    UIViewContentModeLeft, // 视图左侧对齐
    UIViewContentModeRight, // 视图右侧对齐
    UIViewContentModeTopLeft, // 视图左上角对齐
    UIViewContentModeTopRight, // 视图右上角对齐
    UIViewContentModeBottomLeft, // 视图左下角对齐
    UIViewContentModeBottomRight, // 视图右下角对齐
};

// ------------------------------------------------------------------------------------
// 过渡动画效果
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,          // 不使用动画
    UIViewAnimationTransitionFlipFromLeft,  // 从左向右旋转翻页
    UIViewAnimationTransitionFlipFromRight, // 从右向左旋转翻页
    UIViewAnimationTransitionCurlUp,        // 卷曲翻页,从下往上
    UIViewAnimationTransitionCurlDown,      // 卷曲翻页,从上往下
};

// ------------------------------------------------------------------------------------
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    // 不自动调整, 默认值
    UIViewAutoresizingNone                 = 0,
    // 自动调整与superView左边的距离,保证与superView右边的距离不变
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    // 自动调整自己的宽度,保证与superView左边和右边的距离不变
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    // 自动调整与superView右边的距离,保证与superView左边的距离不变
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    // 自动调整与superView顶部的距离,保证与superView底部的距离不变
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    // 自动调整自己的宽度,保证与superView上边和下边的距离不变
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    // 自动调整与superView底部的距离,保证与superView顶部的距离不变
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};

// ------------------------------------------------------------------------------------
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
    // 动画过程中保证子视图跟随运动。提交动画的时候布局子控件,表示子控件将和父控件一同动画。
    UIViewAnimationOptionLayoutSubviews            = 1 <<  0,
    // 动画过程中允许用户交互。
    UIViewAnimationOptionAllowUserInteraction      = 1 <<  1, // turn on user interaction while animating
    // 所有视图从当前状态开始运行。
    UIViewAnimationOptionBeginFromCurrentState     = 1 <<  2, // start all views from current value, not initial value
    // 重复运行动画。
    UIViewAnimationOptionRepeat                    = 1 <<  3, // repeat animation indefinitely
    // 动画运行到结束点后仍然以动画方式回到初始点。执行动画回路,前提是设置动画无限重复
    UIViewAnimationOptionAutoreverse               = 1 <<  4, // if repeat, run animation back and forth
    // 忽略嵌套动画时间设置, 忽略外层动画嵌套的时间变化曲线
    UIViewAnimationOptionOverrideInheritedDuration = 1 <<  5, // ignore nested duration
    // 忽略嵌套动画速度设置 通过改变属性和重绘实现动画效果,如果key没有提交动画将使用快照
    UIViewAnimationOptionOverrideInheritedCurve    = 1 <<  6, // ignore nested curve
    // 动画过程中重绘视图(注意仅仅适用于转场动画)。
    UIViewAnimationOptionAllowAnimatedContent      = 1 <<  7, // animate contents (applies to transitions only)
    // 视图切换时直接隐藏旧视图、显示新视图,而不是将旧视图从父视图移除(仅仅适用于转场动画)**用显隐的方式替代添加移除图层的动画效果**
    UIViewAnimationOptionShowHideTransitionViews   = 1 <<  8, // flip to/from hidden state instead of adding/removing
    // 不继承父动画设置或动画类型。
    UIViewAnimationOptionOverrideInheritedOptions  = 1 <<  9, // do not inherit any options or animation type
    
    // 动画速度控制(可从其中选择一个设置)时间函数曲线相关
    // 动画先缓慢,然后逐渐加速
    UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default
    // 动画逐渐变慢。
    UIViewAnimationOptionCurveEaseIn               = 1 << 16,
    // 动画逐渐加速。
    UIViewAnimationOptionCurveEaseOut              = 2 << 16,
    // 动画匀速执行
    UIViewAnimationOptionCurveLinear               = 3 << 16,
    
    // 转场类型(仅适用于转场动画设置,可以从中选择一个进行设置,基本动画、关键帧动画不需要设置)
    // 没有转场动画效果。
    UIViewAnimationOptionTransitionNone            = 0 << 20, // default
    // 从左侧翻转效果。
    UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
    // 从友侧翻转效果。
    UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
    // 向后翻页的动画过渡效果。
    UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
    // 向前翻页的动画过渡效果。
    UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
    // 旧视图溶解消失显示下一个新视图的效果。
    UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
    // 从上方翻转效果。
    UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
    // 从底部翻转效果。
    UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,
    
    // 默认的帧每秒.
    UIViewAnimationOptionPreferredFramesPerSecondDefault     = 0 << 24,
    // 60帧每秒的帧速率.
    UIViewAnimationOptionPreferredFramesPerSecond60          = 3 << 24,
    // 30帧每秒的帧速率.
    UIViewAnimationOptionPreferredFramesPerSecond30          = 7 << 24,
} NS_ENUM_AVAILABLE_IOS(4_0);

// ------------------------------------------------------------------------------------
typedef NS_OPTIONS(NSUInteger, UIViewKeyframeAnimationOptions) {
    UIViewKeyframeAnimationOptionLayoutSubviews            = UIViewAnimationOptionLayoutSubviews,
    UIViewKeyframeAnimationOptionAllowUserInteraction      = UIViewAnimationOptionAllowUserInteraction, // turn on user interaction while animating
    UIViewKeyframeAnimationOptionBeginFromCurrentState     = UIViewAnimationOptionBeginFromCurrentState, // start all views from current value, not initial value
    UIViewKeyframeAnimationOptionRepeat                    = UIViewAnimationOptionRepeat, // repeat animation indefinitely
    UIViewKeyframeAnimationOptionAutoreverse               = UIViewAnimationOptionAutoreverse, // if repeat, run animation back and forth
    UIViewKeyframeAnimationOptionOverrideInheritedDuration = UIViewAnimationOptionOverrideInheritedDuration, // ignore nested duration
    UIViewKeyframeAnimationOptionOverrideInheritedOptions  = UIViewAnimationOptionOverrideInheritedOptions, // do not inherit any options or animation type
    
    // 连续运算模式, 默认
    UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10, // default
    // 离散运算模式.
    UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 10,
    // 均匀执行运算模式.
    UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 10,
    // 平滑运算模式.
    UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 10,
    // 平滑均匀运算模式.
    UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10
} NS_ENUM_AVAILABLE_IOS(7_0);

// ------------------------------------------------------------------------------------
// 系统动画相关枚举
typedef NS_ENUM(NSUInteger, UISystemAnimation) {
    // 系统自带删除动画
    UISystemAnimationDelete,    // removes the views from the hierarchy when complete 系统自带删除动画
} NS_ENUM_AVAILABLE_IOS(7_0);

// ------------------------------------------------------------------------------------
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
    UIViewTintAdjustmentModeAutomatic, // 自动的,与父视图相同.
    
    UIViewTintAdjustmentModeNormal, // 正常没有修改
    UIViewTintAdjustmentModeDimmed, //tintColor的默认值会自动变得模糊
} NS_ENUM_AVAILABLE_IOS(7_0);

// ------------------------------------------------------------------------------------
typedef NS_ENUM(NSInteger, UISemanticContentAttribute) {
    // 未指定,默认值
    UISemanticContentAttributeUnspecified = 0,
    //  打开/ RW / FF等播放控制按钮
    UISemanticContentAttributePlayback, // for playback controls such as Play/RW/FF buttons and playhead scrubbers
    // 控制导致某种形式的定向改变UI中,如分段控制文本对齐方式或在游戏中方向键
    UISemanticContentAttributeSpatial, // for controls that result in some sort of directional change in the UI, e.g. a segmented control for text alignment or a D-pad in a game
    // 视图总是从左向右布局.
    UISemanticContentAttributeForceLeftToRight,
    // 视图总是从又向左布局.
    UISemanticContentAttributeForceRightToLeft
} NS_ENUM_AVAILABLE_IOS(9_0);

// ------------------------------------------------------------------------------------
// 坐标系空间相关的协议
@protocol UICoordinateSpace 

/** 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id )coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 */
- (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(id )coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect */
- (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(id )coordinateSpace NS_AVAILABLE_IOS(8_0);
/** 将rect从view中转换到当前视图中,返回在当前视图中的rect */
- (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(id )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;

// UIView是iOS系统中界面元素的基础,所有的界面元素都继承自它。UIView本身完全是由CoreAnimation来实现的。它真正的绘图部分是由一个叫CALayer(Core Animation Layer)的类来管理。UIView本身,更像是一个CALayer的管理器,访问它的跟绘图和坐标有关的属性,如frame,bounds等,而内部都是在访问它所包含的CALayer的相关属性。
// UIView继承自UIResponder UIResponder是所有事件的响应基石。

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder 

#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) Class layerClass;                        // default is [CALayer class]. Used when creating the underlying layer for the view.
#else
// UIView有个layer属性,可以返回它的主CALayer实例,UIView有一个layerClass方法,返回主layer所使用的类,UIView的子类可以通过重载这个方法来让UIView使用不同的CALayer来显示
+ (Class)layerClass;   // default is [CALayer class]. Used when creating the underlying layer for the view.
#endif


// 当从代码实例化UIView的时候,initWithFrame会执行;
// 当从文件加载UIView的时候,initWithCoder会执行。
// 初始化方法并且给一个frame
- (instancetype)initWithFrame:(CGRect)frame;          // default initializer

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

// 是否接受用户点击 getter=isUserInterractionEnabled 重构getter方法
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.
// 给UIView加一个tag默认是0
@property(nonatomic)                                 NSInteger tag;                // default is 0
// 返回view的layer
@property(nonatomic,readonly,retain)                 CALayer  *layer;              // returns view's layer. Will always return a non-nil value. view is layer's delegate


/** 返回是否可以成为焦点, 默认NO */
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic,readonly) BOOL canBecomeFocused NS_AVAILABLE_IOS(9_0); // NO by default //是否能被设置为高亮
#else
- (BOOL)canBecomeFocused NS_AVAILABLE_IOS(9_0); // NO by default
#endif

/** 是否可以被聚焦 */
@property (readonly, nonatomic, getter=isFocused) BOOL focused NS_AVAILABLE_IOS(9_0);

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

/** 获取视图的方向 */
// This method returns the layout direction implied by the provided semantic content attribute relative to the application-wide layout direction (as returned by UIApplication.sharedApplication.userInterfaceLayoutDirection).
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute NS_AVAILABLE_IOS(9_0);

/** 获取相对于指定视图的界面方向 */
// This method returns the layout direction implied by the provided semantic content attribute relative to the provided layout direction. For example, when provided a layout direction of RightToLeft and a semantic content attribute of Playback, this method returns LeftToRight. Layout and drawing code can use this method to determine how to arrange elements, but might find it easier to query the container view’s effectiveUserInterfaceLayoutDirection property instead.
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute relativeToLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection NS_AVAILABLE_IOS(10_0);

/** 返回即时内容的布局的方向 */
// Returns the user interface layout direction appropriate for arranging the immediate content of this view. Always consult the effectiveUserInterfaceLayoutDirection of the view whose immediate content is being arranged or drawn. Do not assume that the value propagates through the view’s subtree.
@property (readonly, nonatomic) UIUserInterfaceLayoutDirection effectiveUserInterfaceLayoutDirection NS_AVAILABLE_IOS(10_0);


@end

你可能感兴趣的:(UIView 半解(1))