所謂動畫效果,就是會動的畫,到iOS App中來說的話,就是各種UIView的移動。 想想看,如果我們自己來實現所有UIView的動畫效果,需要考慮些什么東西呢?
* 該UIView現在在哪兒?
* 該UIView最后會動到哪兒?
* 該UIView以什么樣的方式移動到那兒?
* 該動畫持續多長時間?
* 每次移動的最小時間間隔?
* 每次最小時間間隔的移動的應該移動到哪兒?
* ….
想想這是一個多么殺腦細胞的過程,尤其是每一次的動畫過程都要重復這一折磨的過程。
還好,現實比想象的美好, 蘋果公司為開發者思考了上面的問題,通過使用UIKit提供的動畫支持,開發者只需要簡單的幾行代碼就能實現各種各樣的動畫效果。在UIKit中,所有的動畫效果支持的方法都在UIView類中。
首先,在UIView中有很多屬性用以描述一個UIView的狀態,而動畫就是讓UIView從一個狀態平滑的過渡到另外一個狀態的過程。這些屬性有:
通過設置這些屬性,基本上就解決了動畫中的移動到哪兒的問題。
接著,蘋果公司在UIView中加入很多方法來方便家控制動畫的移動時間,以及移動的方式。iOS3.0及之前,UIView支持的Animation方法有如下這么多:
Object-c代碼
@interface UIView(UIViewAnimation)
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // additional context info passed to will start/did stop selectors. begin/commit can be nested
+ (void)commitAnimations; // starts up any animations when the top level animation is commited
// no getters. if called outside animation block, these setters have no effect.
+ (void)setAnimationDelegate:(id)delegate; // default = nil
+ (void)setAnimationWillStartSelector:(SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).
新聞熱點
疑難解答