接續上一篇的屬性篇,這次進入 UIView 的方法。類方法這塊基本上被動畫佔滿了,以下依功能分組說明。
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations;
兩者搭配使用。addKeyframeWithRelativeStartTime: 的兩個時間參數都是「倍數」而非秒數,範圍 0 到 1。假設整體動畫長 2 秒,把 frameStartTime 設為 0.5,代表這段動畫會在整體進行到 1 秒時才開始;frameDuration 同理,是佔整體時長的比例。
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations;
+ (void)animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
三個版本逐步增加能力:第二個多了完成回呼,第三個再加上延遲與動畫曲線等選項。
+ (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;
dampingRatio 控制阻尼比例,值越小回彈越明顯,接近 1 則幾乎沒有回彈;velocity 是初始速度。
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; + (void)commitAnimations;
beginAnimations 開始一個動畫區塊,context 可攜帶要傳給動畫代理的應用資訊;commitAnimations 結束區塊,若這是最外層的區塊,動畫會在應用回到 run loop 時啟動。動畫在獨立執行緒中進行,不會中斷應用。透過這組 API,多個動畫可以被合併成一個區塊,也能搭配 setAnimationBeginsFromCurrentState: 決定要不要從目前狀態續播。
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // 速度曲線:先快後慢等
+ (void)setAnimationDelay:(NSTimeInterval)delay;
+ (void)setAnimationDelegate:(id)delegate;
+ (void)setAnimationWillStartSelector:(SEL)selector;
+ (void)setAnimationDidStopSelector:(SEL)selector;
+ (void)setAnimationDuration:(NSTimeInterval)duration;
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
+ (void)setAnimationRepeatCount:(float)repeatCount;
+ (void)setAnimationsEnabled:(BOOL)enabled;
+ (void)setAnimationStartDate:(NSDate *)startTime;
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition
forView:(UIView *)view cache:(BOOL)cache;
這些 setter 只有在動畫區塊「內部」呼叫才有效,寫到 beginAnimations 之外不會有任何效果——這是舊式 API 最常踩的坑。willStart 與 didStop 選擇器分別要符合 animationDidStart:context: 與 animationDidStop:finished:context: 的簽名。
setAnimationTransition 的 cache 參數:傳 YES 時,系統會在動畫開始與結束各渲染一次並在過程中重用影格,效能較好,適合中途不需要更新內容的轉場;傳 NO 則每一格都重新渲染。
+ (void)transitionWithView:(UIView *)view
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
+ (void)transitionFromView:(UIView *)fromView
toView:(UIView *)toView
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
completion:(void (^)(BOOL finished))completion;
前者對單一視圖套用轉場效果,後者則是在兩個視圖之間做切換。
+ (Class)layerClass; // 回傳用於建立 layer 的類別
+ (void)performSystemAnimation:(UISystemAnimation)animation
onViews:(NSArray *)views
options:(UIViewAnimationOptions)options
animations:(void (^)(void))parallelAnimations
completion:(void (^)(BOOL finished))completion;
+ (void)performWithoutAnimation:(void (^)(void))actionsWithoutAnimation;
+ (BOOL)areAnimationsEnabled;
+ (BOOL)requiresConstraintBasedLayout;
performSystemAnimation:onViews: 在一組視圖上執行系統動畫,parallelAnimations 是與系統動畫同時進行的自訂動畫;performWithoutAnimation: 則是暫時關掉動畫執行一段程式碼,常用於「重置狀態但不要有過渡」。
以上就是 UIView 全部的類方法。若有錯誤,歡迎指正。—— LC