一.基本方式:使用UIView類的UIViewAnimation擴充
函數說明
+ (void)beginAnimations:(NSString *)animationID context:(void *)context; // 開始準備動畫
+ (void)commitAnimations; // 運行動畫
// 沒有get方法,下面的set在快外調用無效
+ (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. YES的話,動畫(非最後一次)結束後動態複原到最開始狀態
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. 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).
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache; // 添加動畫到view上,cache是YES的時候比較高效,但是動畫過程中不能更新介面上的內容,NO時每一幀都重新畫,可以即時更新
+ (void)setAnimationsEnabled:(BOOL)enabled; // 是否忽略一些動畫設定
+ (BOOL)areAnimationsEnabled;
一個動畫的代碼
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:2.7];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
// operation>>>
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
// end<<<<<<
[UIView commitAnimations];
其中transition取值範圍
typedef enum {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;
特點:基礎,使用方便,但是效果有限
二.block方式:使用UIView類的UIViewAnimationWithBlocks擴充
函數說明
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);//間隔,延遲,動畫參數(好像沒用?),介面更改塊,結束塊
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0);
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // toView added to fromView.superview, fromView removed from its superview介面替換,這裡的options參數有效
舉例:
[UIView animateWithDuration:0.7 delay:0 options:0 animations:^(){
self.view.alpha = 0.2;
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
self.view.alpha = 1;
} completion:^(BOOL finished)
{
}];
當areAnimationsEnabled為NO時,上面不能動畫顯示
[UIView transitionFromView:lImage toView:mImage duration:0.7 options:options completion:^(BOOL finished)
{
if (finished) {
[self.view addSubview:lImage];
[self.view sendSubviewToBack:lImage];
[self.view sendSubviewToBack:mImage];
}
}];
options取值範圍
enum {
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
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
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,//ios5
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,//ios5
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,//ios5
};
typedef NSUInteger UIViewAnimationOptions;
特點:快捷方便,效果更多.可以如上樣本1那樣實現介面個元素屬性漸進變化的動態展示
三.core方式:使用CATransition類
使用要引入QuartzCore.framework
官方有個樣本:ViewTransitions
基本上就是
CATransition *transition = [CATransition animation];
transition.duration = 0.7;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;//{kCATransitionMoveIn, kCATransitionPush, kCATransitionReveal, kCATransitionFade};
//更多私人{@"cube",@"suckEffect",@"oglFlip",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose"};
transition.subtype = kCATransitionFromLeft;//{kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom};
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
// 要做的
[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
其中需要注意的是:介面調整(動畫前後展示的兩個介面)需要在動畫時間內完成,否則沒有效果,對下次也有影響,我測的不是太完整,大家自己寫個performSelector: withObject: afterDelay:實驗下看看就知道.
特點:動畫效果較多(但是私人),可以實現UIViewController之間的動畫
demo:animations.zip
轉自:http://www.cnblogs.com/v2m_/archive/2011/10/28/2227979.html