iOS開發之各種動畫各種頁面切面效果,ios開發頁面切面
因工作原因,有段時間沒發表部落格了,今天就發表篇部落格給大家帶來一些乾貨,切勿錯過哦。今天所介紹的主題是關於動畫的,在之前的部落格中也有用到動畫的地方,今天就好好的總結一下iOS開發中常用的動畫。說道動畫其中有一個是仿射變換的概念,至於怎麼仿射的怎麼變換的,原理如何等在本篇部落格中不做贅述。今天要分享的是如和用動畫做出我們要做的效果。
今天主要用到的動畫類是CALayer下的CATransition至於各種動畫類中如何繼承的在這也不做贅述,網上的資料是一抓一大把。好廢話少說切入今天的正題。
一.封裝動畫方法
1.用CATransition實現動畫的封裝方法如下,每句代碼是何意思,請看注釋之。
1 #pragma CATransition動畫實現 2 - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view 3 { 4 //建立CATransition對象 5 CATransition *animation = [CATransition animation]; 6 7 //設定運動時間 8 animation.duration = DURATION; 9 10 //設定運動type11 animation.type = type;12 if (subtype != nil) {13 14 //設定子類15 animation.subtype = subtype;16 }17 18 //設定運動速度19 animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;20 21 [view.layer addAnimation:animation forKey:@"animation"];22 }
代碼說明:
CATransition常用的屬性如下:
duration:設定動畫時間
type:稍後下面會詳細的介紹運動類型
subtype:和type匹配使用,指定運動的方向,下面也會詳細介紹
timingFunction :動畫的運動軌跡,用於變化起點和終點之間的插值計算,形象點說它決定了動畫啟動並執行節奏,比如是
均勻變化(相同時間變化量相同)還是先快後慢,先慢後快還是先慢再快再慢.
* 動畫的開始與結束的快慢,有五個預置分別為(下同):
* kCAMediaTimingFunctionLinear 線性,即勻速
* kCAMediaTimingFunctionEaseIn 先慢後快
* kCAMediaTimingFunctionEaseOut 先快後慢
* kCAMediaTimingFunctionEaseInEaseOut 先慢後快再慢
* kCAMediaTimingFunctionDefault 實際效果是動畫中間比較快.
2.用UIView的block回調實現動畫的代碼封裝
1 #pragma UIView實現動畫2 - (void) animationWithView : (UIView *)view WithAnimationTransition : (UIViewAnimationTransition) transition3 {4 [UIView animateWithDuration:DURATION animations:^{5 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];6 [UIView setAnimationTransition:transition forView:view cache:YES];7 }];8 }
3.改變View的背景圖,便於切換時觀察
1 #pragma 給View添加背景圖2 -(void)addBgImageWithImageName:(NSString *) imageName3 {4 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];5 }
二.調用上面的方法實現我們想要的動畫
1.我們在View上添加多個Button,給不同的Button設定不同的Tag值,然後再ViewController中綁定同一個方法,點擊不同的button實現不同的頁面轉場效果。storyBoard上的控制項效果如所示:
1 typedef enum : NSUInteger { 2 Fade = 1, //淡入淡出 3 Push, //推擠 4 Reveal, //揭開 5 MoveIn, //覆蓋 6 Cube, //立方體 7 SuckEffect, //吮吸 8 OglFlip, //翻轉 9 RippleEffect, //波紋10 PageCurl, //翻頁11 PageUnCurl, //反翻頁12 CameraIrisHollowOpen, //開鏡頭13 CameraIrisHollowClose, //關鏡頭14 CurlDown, //下翻頁15 CurlUp, //上翻頁16 FlipFromLeft, //左翻轉17 FlipFromRight, //右翻轉18 19 } AnimationType;
(2),擷取Button的Tag值:
1 UIButton *button = sender;2 AnimationType animationType = button.tag;
(3).每次點擊button都改變subtype的值,包括上,左,下,右
1 NSString *subtypeString; 2 3 switch (_subtype) { 4 case 0: 5 subtypeString = kCATransitionFromLeft; 6 break; 7 case 1: 8 subtypeString = kCATransitionFromBottom; 9 break;10 case 2:11 subtypeString = kCATransitionFromRight;12 break;13 case 3:14 subtypeString = kCATransitionFromTop;15 break;16 default:17 break;18 }19 _subtype += 1;20 if (_subtype > 3) {21 _subtype = 0;22 }
(4),通過switch結合上邊的枚舉來判斷是那個按鈕點擊的
1 switch (animationType)2 {3 //各種Case,此處代碼下面會給出 4 }
3.調用我們封裝的運動方法,來實現動畫效果
(1),淡化效果
1 case Fade:2 [self transitionWithType:kCATransitionFade WithSubtype:subtypeString ForView:self.view];3 break;4
(2).Push效果
1 case Push:2 [self transitionWithType:kCATransitionPush WithSubtype:subtypeString ForView:self.view];3 break;4
效果如下:
(3).揭開效果:
1 case Reveal:2 [self transitionWithType:kCATransitionReveal WithSubtype:subtypeString ForView:self.view];3 break;
如下:
(4).覆蓋效果
1 case MoveIn:2 [self transitionWithType:kCATransitionMoveIn WithSubtype:subtypeString ForView:self.view];3 break;4
如下:
(5).立方體效果
1 case Cube:2 [self transitionWithType:@"cube" WithSubtype:subtypeString ForView:self.view];3 break;
效果如下:
(6).吮吸效果
1 case SuckEffect:2 [self transitionWithType:@"suckEffect" WithSubtype:subtypeString ForView:self.view];3 break;
效果如下:
(7).翻轉效果
1 case OglFlip:2 [self transitionWithType:@"oglFlip" WithSubtype:subtypeString ForView:self.view];3 break;
圖如下:
8.波紋效果
1 case RippleEffect:2 [self transitionWithType:@"rippleEffect" WithSubtype:subtypeString ForView:self.view];3 break;
(9).翻頁和反翻頁效果
1 case PageCurl:2 [self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view];3 break;4 5 case PageUnCurl:6 [self transitionWithType:@"pageUnCurl" WithSubtype:subtypeString ForView:self.view];7 break;
(10).相機開啟效果
1 case CameraIrisHollowOpen:2 [self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:subtypeString ForView:self.view];3 break;4 5 case CameraIrisHollowClose:6 [self transitionWithType:@"cameraIrisHollowClose" WithSubtype:subtypeString ForView:self.view];7 break;
(11),調用上面封裝的第二個動畫方法
1 case CurlDown: 2 [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlDown]; 3 break; 4 5 case CurlUp: 6 [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlUp]; 7 break; 8 9 case FlipFromLeft:10 [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromLeft];11 break;12 13 case FlipFromRight:14 [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromRight];15 break;
我把上面的Demo的源碼放在GitHub上,其地址為:https://github.com/lizelu/CATransitionDemo.git
今天的部落格就先到這裡吧,以後有時間還會更新內容的,敬請關注哦!!