UIView 是 iOS 所有界面元素的基礎類別,幾乎每一個 you看得到的控件都繼承自它。有趣的是,UIView 本身完全由 Core Animation 實作,真正的繪圖工作交由名為 CALayer(Core Animation Layer)的類別管理。
換個說法:UIView 更像是它所含 CALayer 的管理器。當你存取 frame、bounds 等與繪圖、座標相關的屬性時,內部實際上都是在存取 CALayer 的對應屬性。理解這層關係,動畫與渲染的許多行為就變得可以預期。
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // 首尾較慢
UIViewAnimationCurveEaseIn, // 起始較慢
UIViewAnimationCurveEaseOut, // 結尾較慢
UIViewAnimationCurveLinear // 等速
};
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, // 拉伸填滿
UIViewContentModeScaleAspectFit, // 等比例縮至可容納,餘處透明
UIViewContentModeScaleAspectFill, // 等比例放大至填滿,可能裁切
UIViewContentModeRedraw, // bounds 改變即重繪(觸發 setNeedsDisplay)
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, // 不自動調整
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, // 左邊距可變
UIViewAutoresizingFlexibleWidth = 1 << 1, // 寬度可變
UIViewAutoresizingFlexibleRightMargin = 1 << 2, // 右邊距可變
UIViewAutoresizingFlexibleTopMargin = 1 << 3, // 頂邊距可變
UIViewAutoresizingFlexibleHeight = 1 << 4, // 高度可變
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 // 底邊距可變
};
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
} 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(NSInteger, UIViewTintAdjustmentMode) {
UIViewTintAdjustmentModeAutomatic,
UIViewTintAdjustmentModeNormal,
UIViewTintAdjustmentModeDimmed
} NS_ENUM_AVAILABLE_IOS(7_0);
@protocol UICoordinateSpace <NSObject> - (CGPoint)convertPoint:(CGPoint)point toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace; - (CGPoint)convertPoint:(CGPoint)point fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace; - (CGRect)convertRect:(CGRect)rect toCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace; - (CGRect)convertRect:(CGRect)rect fromCoordinateSpace:(id <UICoordinateSpace>)coordinateSpace; @property (readonly, nonatomic) CGRect bounds; @end
NS_CLASS_AVAILABLE_IOS(2_0)
@interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer,
UIDynamicItem, UITraitEnvironment, UICoordinateSpace>
+ (Class)layerClass; // 預設 [CALayer class];子類可覆寫以使用自訂 layer
- (instancetype)initWithFrame:(CGRect)frame;
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // 預設 YES
@property(nonatomic) NSInteger tag; // 預設 0
@property(nonatomic, readonly, retain) CALayer *layer; // 永遠非 nil,view 為 layer 的 delegate
@end
@interface UIView(UIViewGeometry) @property(nonatomic) CGRect frame; // 位置與尺寸,以父視圖左上角為原點 @property(nonatomic) CGRect bounds; // 以自身左上角為原點,可動畫 @property(nonatomic) CGPoint center; // frame 中心點,可動畫 @property(nonatomic) CGAffineTransform transform; // 預設 CGAffineTransformIdentity @property(nonatomic) CGFloat contentScaleFactor NS_AVAILABLE_IOS(4_0); @property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled; // 預設 NO @property(nonatomic, getter=isExclusiveTouch) BOOL exclusiveTouch; // 為 YES 時此視圖成為第一響應者後,手指離開螢幕前其他視圖不會收到任何 touch 事件 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // 遞迴呼叫 pointInside:withEvent:,point 使用接收者的座標系 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event; - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view; - (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view; - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view; - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view; @property(nonatomic) BOOL autoresizesSubviews; @property(nonatomic) UIViewAutoresizing autoresizingMask; - (CGSize)sizeThatFits:(CGSize)size; // 回傳最合適尺寸,不會真的改變 view - (void)sizeToFit; // 以 bounds 呼叫 sizeThatFits: 並套用結果 @end
hitTest:withEvent: 有一個經典應用:在視圖上放置遮罩層卻不阻擋下方操作,做法是覆寫它,讓遮罩範圍以外的點直接回傳 nil。
@interface UIView(UIViewHierarchy) @property(nonatomic, readonly) UIView *superview; @property(nonatomic, readonly, copy) NSArray *subviews; @property(nonatomic, readonly) UIWindow *window; - (void)removeFromSuperview; - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; - (void)addSubview:(UIView *)view; // 新加入者在 subviews 尾端,也就是最上層 - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; - (void)bringSubviewToFront:(UIView *)view; - (void)sendSubviewToBack:(UIView *)view; // 通知型方法,預設不做任何事,子類可覆寫 - (void)didAddSubview:(UIView *)subview; - (void)willRemoveSubview:(UIView *)subview; - (void)willMoveToSuperview:(UIView *)newSuperview; - (void)didMoveToSuperview; - (void)willMoveToWindow:(UIWindow *)newWindow; - (void)didMoveToWindow; - (BOOL)isDescendantOfView:(UIView *)view; // 對自己回傳 YES - (UIView *)viewWithTag:(NSInteger)tag; // 遞迴搜尋,包含自己 - (void)setNeedsLayout; // 標記需要佈局,下一個週期處理 - (void)layoutIfNeeded; // 若有標記則立即佈局 - (void)layoutSubviews; // 覆寫點,由 layoutIfNeeded 自動呼叫 // iOS 8 起可定義視圖間距,僅對 Auto Layout 有效 @property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0); @property (nonatomic) BOOL preservesSuperviewLayoutMargins NS_AVAILABLE_IOS(8_0); - (void)layoutMarginsDidChange NS_AVAILABLE_IOS(8_0); @end
@interface UIView(UIViewRendering) - (void)drawRect:(CGRect)rect; // 不可直接呼叫 - (void)setNeedsDisplay; // 標記需要重繪 - (void)setNeedsDisplayInRect:(CGRect)rect; // 標記局部重繪 @property(nonatomic) BOOL clipsToBounds; // 子視圖超出邊界時是否裁切,預設 NO @property(nonatomic, copy) UIColor *backgroundColor; @property(nonatomic) CGFloat alpha; // 預設 1.0 @property(nonatomic, getter=isOpaque) BOOL opaque; // 預設 YES @property(nonatomic) BOOL clearsContextBeforeDrawing; // 預設 YES,不透明視圖會忽略 @property(nonatomic, getter=isHidden) BOOL hidden; @property(nonatomic) UIViewContentMode contentMode; // 預設 ScaleToFill @property(nonatomic, retain) UIView *maskView NS_AVAILABLE_IOS(8_0); @property(nonatomic, retain) UIColor *tintColor NS_AVAILABLE_IOS(7_0); @property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0); - (void)tintColorDidChange NS_AVAILABLE_IOS(7_0); @end
tintColor 是 iOS 7 引入的外觀統一機制:預設為 nil 時會沿著父視圖鏈向上尋找,若都沒有設定則使用系統藍色。因此設定根控制器的 tintColor 就能改變整個 App 的主色調。tintAdjustmentMode 為 Dimmed 時,回傳的 tintColor 會自動變暗,適合在選取或停用狀態使用。
@interface UIView(UIViewAnimation)
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 可巢狀
+ (void)commitAnimations;
+ (void)setAnimationDelegate:(id)delegate;
+ (void)setAnimationWillStartSelector:(SEL)selector;
+ (void)setAnimationDidStopSelector:(SEL)selector;
+ (void)setAnimationDuration:(NSTimeInterval)duration; // 預設 0.2
+ (void)setAnimationDelay:(NSTimeInterval)delay;
+ (void)setAnimationStartDate:(NSDate *)startDate;
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;
+ (void)setAnimationRepeatCount:(float)repeatCount; // 可為小數
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition
forView:(UIView *)view cache:(BOOL)cache;
+ (void)setAnimationsEnabled:(BOOL)enabled;
+ (BOOL)areAnimationsEnabled;
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation
NS_AVAILABLE_IOS(7_0);
@end
@interface UIView(UIViewAnimationWithBlocks)
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
NS_AVAILABLE_IOS(4_0);
// 彈簧動畫:dampingRatio 為 1 時平滑收束不震盪,小於 1 時會來回震盪
// velocity 以「每秒移動多少比例總距離」為單位,通常傳 0
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity
options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView
duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
// 系統提供的動畫(如鍵盤彈出),可搭配平行動畫
+ (void)performSystemAnimation:(UISystemAnimation)animation onViews:(NSArray *)views
options:(UIViewAnimationOptions)options animations:(void (^)(void))parallelAnimations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
@end
@interface UIView (UIViewKeyframeAnimations)
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
// 加入序列中的關鍵點,時間與 duration 皆為 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(nonatomic, copy) NSArray *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 *motionEffects NS_AVAILABLE_IOS(7_0);
@end
gestureRecognizerShouldBegin: 預設回傳 YES;回傳 NO 會讓該手勢直接進入 Failed 狀態。UISlider 就是靠它阻止與滑桿平行且起於滑柄的滑動手勢。
typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {
UILayoutConstraintAxisHorizontal = 0,
UILayoutConstraintAxisVertical = 1
};
@interface UIView (UIConstraintBasedLayoutInstallingConstraints)
- (NSArray *)constraints NS_AVAILABLE_IOS(6_0);
- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);
- (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0);
- (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0);
- (void)removeConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0);
@end
約束通常安裝在參與約束的所有視圖最近的共同祖先上。約束中的數值以「安裝它的視圖」座標系解讀,且一個視圖被視為自己的祖先。
渲染一個視窗時,會依序進行三個階段(必要時才執行):update constraints → layout → display。
這三步並非單向線性:若在 layout 過程中改變了 constraint,就會觸發新一輪的 update constraints 迭代。實作時必須避免在此形成迴圈。
@interface UIView (UIConstraintBasedLayoutCoreMethods) // 觸發約束更新,僅在 needsUpdateConstraints 為 YES 時才會成功。不應覆寫此方法 - (void)updateConstraintsIfNeeded NS_AVAILABLE_IOS(6_0); // 自訂視圖時覆寫此處以設定約束,務必呼叫 [super updateConstraints] - (void)updateConstraints NS_AVAILABLE_IOS(6_0); - (BOOL)needsUpdateConstraints NS_AVAILABLE_IOS(6_0); - (void)setNeedsUpdateConstraints NS_AVAILABLE_IOS(6_0); @end
@interface UIView (UIConstraintBasedCompatibility) // 預設 YES。以程式撰寫約束時必須設為 NO,否則 autoresizing mask 產生的約束會衝突 - (BOOL)translatesAutoresizingMaskIntoConstraints NS_AVAILABLE_IOS(6_0); - (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag NS_AVAILABLE_IOS(6_0); + (BOOL)requiresConstraintBasedLayout NS_AVAILABLE_IOS(6_0); @end @interface UIView (UIConstraintBasedLayoutLayering) // 約束作用的對象是 alignment rect,而非 frame - (CGRect)alignmentRectForFrame:(CGRect)frame NS_AVAILABLE_IOS(6_0); - (CGRect)frameForAlignmentRect:(CGRect)alignmentRect NS_AVAILABLE_IOS(6_0); - (UIEdgeInsets)alignmentRectInsets NS_AVAILABLE_IOS(6_0); - (UIView *)viewForBaselineLayout NS_AVAILABLE_IOS(6_0); @end
alignment rect 的意義是「使用者眼中控件真正的邊界」,排除了陰影、描邊等裝飾。若你的自訂視圖有這類裝飾,覆寫 alignmentRectInsets 通常比覆寫前兩個方法更簡單也不易出錯。
UIKIT_EXTERN const CGFloat UIViewNoIntrinsicMetric NS_AVAILABLE_IOS(6_0); // -1
- (CGSize)intrinsicContentSize NS_AVAILABLE_IOS(6_0);
- (void)invalidateIntrinsicContentSize NS_AVAILABLE_IOS(6_0);
// 當影響固有尺寸的內容改變時必須呼叫,否則 UIKit 不會察覺變化
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis;
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis;
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority
forAxis:(UILayoutConstraintAxis)axis;
@end
以按鈕為例,系統預設行為是:垂直方向強烈貼合內容(按鈕應該是自然高度)、水平方向微弱貼合(左右多一點內距可接受)、兩個方向都強烈抵抗壓縮或裁切。若你的場景更希望「文字截斷但按鈕全部顯示」,就把水平方向的 CompressionResistance 調降。
UIKIT_EXTERN const CGSize UILayoutFittingCompressedSize NS_AVAILABLE_IOS(6_0); // 最小
UIKIT_EXTERN const CGSize UILayoutFittingExpandedSize NS_AVAILABLE_IOS(6_0); // 最大
@interface UIView (UIConstraintBasedLayoutFittingSize)
- (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 (UIConstraintBasedLayoutDebugging)
// 回傳所有影響目前位置的約束(可能間接影響,不一定直接涉及自身)
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
// 約束不足以唯一決定佈局時即為「不確定佈局」,症狀是視圖位置跳動或錯位
- (BOOL)hasAmbiguousLayout NS_AVAILABLE_IOS(6_0);
- (void)exerciseAmbiguityInLayout NS_AVAILABLE_IOS(6_0); // 隨機切換到另一個合法佈局,方便定位
@end
hasAmbiguousLayout 與 exerciseAmbiguityInLayout 只能應用於除錯,切勿留在正式出貨的程式碼中。
@interface UIView (UISnapshotting)
- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);
- (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
@interface UIView (UIStateRestoration)
@property (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
使用快照時有個容易踩的陷阱:若在 layoutSubviews 中呼叫 snapshotViewAfterScreenUpdates:YES 並同時修改 alpha,快照會連該次修改一起擷取,結果看起來是空的。若要在佈局期間做動畫,應該對快照做動畫,而非對原視圖。
把這份參考當索引使用即可。日常開發真正高頻的只有少數幾項:frame 與 center、addSubview、layer 相關設定、animateWithDuration: 系列、以及 Auto Layout 的約束安裝與 updateConstraints 覆寫。其餘在遇到具體問題時再回查,效率更高。