iOS:動畫,ios動畫
1、UIView Animation
1-1)、原始的,非Block。
//動畫名、內容[UIView beginAnimations:@"id100" context:@"animation1"];//時間長度2秒[UIView setAnimationDuration:2.0];//開始緩慢,不會勻速// UIViewAnimationCurveEaseInOut, // 慢入、慢出// UIViewAnimationCurveEaseIn, // 慢入// UIViewAnimationCurveEaseOut, // 慢出// UIViewAnimationCurveLinear, // 勻速[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];//設定代理[UIView setAnimationDelegate:self];//動畫將要開始SEL[UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];//動畫將要結束SEL[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];//提交動畫[UIView commitAnimations];//簽代理後,才能實現SEL的方法(方法名隨意,參數個數也隨意,分別少於2/3個的時候,只收到前面幾個參數,多於它的參數,後面參數空,有用過C的回呼函數,應該會比較熟悉)//開始動畫時的方法-(void)animationWillStart:(NSString *)animationID context:(void *)context{ NSLog(@"動畫開始");}//結束動畫時的方法-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{ //可以判斷ID,做連續動畫 NSLog(@"動畫結束");}//自己取名-(void)theAnimationStop:(NSString *)animationID thecontext:(void *)theContext context:(void *)context2{NSLog(@"%@,%@,%@",animationID,theContext,context2);}
1-2)、Block
//嵌套,做連續動畫[UIView animateWithDuration:2.0 animations:^{ self.cyanVIew.frame = CGRectMake(100, 400, 100, 100);} completion:^(BOOL finished) { [UIView animateWithDuration:2.0 animations:^{ self.cyanVIew.frame = CGRectMake(100, 100, 100, 100); }];}];
2、Transition(轉場動畫)
2-1)、原始的
//建立CATransition *animation = [CATransition animation];//動畫時間animation.duration = 1.5;//動畫類型// kCATransitionFade //淡入// kCATransitionMoveIn //覆蓋// kCATransitionPush //推// kCATransitionReveal //掀起,相對覆蓋// @"cube" //立方體(某寶的AR切換)// @"suckEffect" //吮吸// @"oglFlip" //翻轉(某信,好友曆史說說,查看詳情)// @"rippleEffect" //波紋// @"pageCurl" //日曆上翻// @"pageUnCurl" //日曆下蓋// @"cameraIrisHollowOpen" //相機開啟// @"cameraIrisHollowClose" //相機關閉animation.type = @"cube";//動畫方向// kCATransitionFromRight //從右邊// kCATransitionFromLeft //從左邊// kCATransitionFromTop //從上面// kCATransitionFromBottom //從下面animation.subtype = kCATransitionFromLeft;//動畫速度(慢入、慢出)// kCAMediaTimingFunctionLinear //勻速// kCAMediaTimingFunctionEaseIn //慢入// kCAMediaTimingFunctionEaseOut //慢出// kCAMediaTimingFunctionEaseInEaseOut //慢入慢出// kCAMediaTimingFunctionDefault //勻速,不過有點快animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];//代理//- (void)animationDidStart:(CAAnimation *)anim;//- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;animation.delegate = self;//1、添加動畫到導覽列[self.navigationController.view.layer addAnimation:animation forKey:@"animation"];[self.navigationController pushViewController:vc animated:NO];//2、添加動畫到普通的View[bgView.layer addAnimation:animation forKey:@"animation"];//把最上面的View放到最底層,一般轉場可能就2層相互轉換,也可用exchangeSubviewAtIndex:withSubviewAtIndex:[bgView sendSubviewToBack:[[bgView subviews] lastObject]];
2-2)、封裝過的,簡潔,用UIView Animation,View的方法,非Layer
//參數cache,YES為後再轉場,減輕系統負擔,動畫更流暢,NO為一起動畫,如需要邊轉場邊動畫的效果// UIViewAnimationTransitionNone,// UIViewAnimationTransitionFlipFromLeft, //左邊下翻效果(X信,好友曆史說說,查看詳情)// UIViewAnimationTransitionFlipFromRight, //右邊下翻效果// UIViewAnimationTransitionCurlUp, //上翻日曆效果// UIViewAnimationTransitionCurlDown, //下蓋日曆效果//1、導覽列轉場[UIView animateWithDuration:2.0 animations:^{ [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES]; [self.navigationController pushViewController:vc animated:NO];}];//2、普通View轉場,把當前View放在最底層,最好大小全相同,不然動畫效果很尷尬[UIView animateWithDuration:2.0 animations:^{ //轉場動畫 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:bgView cache:YES]; //最上面的View放到最下面 [bgView sendSubviewToBack:[[bgView subviews] lastObject]];}];
2-3)、VC內建的模態視圖轉場動畫
//設定模態視圖的轉場效果(如X信,朋友的曆史單條說說,點擊查看詳細)。second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//推[self presentViewController:second animated:YES completion:^{}];