iOS學習階段總結-b20120920-動畫轉場
iPhone動畫型別:
1.UIView,可能在底層用CATransition進行了封裝,它只能用於一些簡單的常用的效果展現;
2.CATransition,相對複雜,是對UIView的Layer進行底層控制和動畫操作。
UIView基本動畫轉場
第一類UIView類方法全域性:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimaitonCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:5.0f];
/*動作放在這裡*/
[UIView commitAnimations];
淡進淡出:alpha變換
...
淡進:imgeView.alpha = 1.0f 淡出:iamgeView.alpha = 0.0f
...
位移:位置變換
view.frame = CGRectMake(0.0f,0.0f,320.0f,44.0f);
...
view.frame = CGRectMake(0.0f,44.0f,320.0f,44.0f);
...
旋轉變換
...
view.transform = CGAffineTransformMakeRotation(3.14/2.0);
...
核心方法:
+ (void)setAnimationDelegate:(id)delegate;//設定代理,預設為nil
+ (void)setAnimationWillSelector:(SEL)selector;//設定動畫開始時的呼叫方法,預設為NULL
+ (void)setAnimationDidStopSelector:(SEL)selector;//設定動畫停止時的呼叫方法,預設為NULL
+ (void)setAnimationDuration:(NSTimerInterval)duration;//設定動畫持續時間按,預設是0.2秒
+ (void)setAnimationDelay:(NSTimeInterval)delay;//設定動畫的開始時間(延遲時間),預設是0.0
+ (void)setAnimationStartDate:(NSDate *)startDate;//設定動畫的開啟日期,預設是NULL
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;//設定動畫的方式,預設是UIViewAnimationCurveEaseInOut;
+ (void)setAnimationRepeatCount:(float)repeatCount;//設定動畫的重複次數,預設是0
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;//設定動畫完成後是否還原,結束後動態復原到最開始狀態,預設是NO
iOS4.0之後的Blocks動畫
UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:3.0f delay:0.0 options:options animations:^{} completion:^(BOOL finished){}];
核心方法;
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations completion:(void(^)(BOOL finished))completion;
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void(^)(void))animations;
第二類UIView動畫
支援UIView翻頁CurlUp/CurlDown
支援UIView翻轉FlipFromLeft/FlipFromRight
翻頁程式碼
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:5.0f];
[UIView setAnimationTransition:UIViewAnimationTransitionCulUp forView:parentView cache:YES];//設定parentView翻頁動畫
[parentView exchangeSubviewAtIndex:page1Index withSubviewAtIndex:page2Index];//把parentView中的page1和page2換一個位置
[UIView commitAnimations];
使用Block的翻頁程式碼
UIViewAnimatinoOptions options = UIViewAnimationOptionTransitionCurlUp | UIViewAnimationOptionCurveEaseInOut;
[UIView transitionWithView:parentView
duration:3.0f options:options
animations:^{
[parentView exchangeSubviewAtIndex:page1Index withSubviewAtIndex:page2Index];
}
completion:^(BOOL completion){
NSLog(@"finished %d",finished);
}
CATransition動畫轉場
對於UIView使用:
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitonFade;
animation.subtype = kCATransitionFromRight;
[view exchangeSubviewAtIndex:page1Index withSubviewAtIndex:page2Index];
[view.layer addAnimation:animation forKey:@"animation"];
對月ViewController使用
CATransition *ca = [CATransition animation];
[ca setType:@"cube"];
[ca setSubtype:kCATransitionFromRight];
[ca setDuration:1.0f];
[self.navigationController.view.layer addAnimation:ca forKey:@"test"];
[self.navigationController pushViewController:svc animated:YES];
[svc release];
animation.type型別
kCATransitionFade,kCATransitionMoveIn,kCATransitionPush,kCATransitionReveal
animation.subtype型別
kCATransitionFromRight,kCATransitionFromLeft,kCATransitionFromTop,kCATransitionFromButtom
CATransition包含了若干(type)私有函式,分別是
cube,suckEffect,oglFlip,rippleEffect,pageCurl,pageUnCurl,cameralrisHollowOpen,camralrisHollowClose