因工作原因,有段時(shí)間沒(méi)發(fā)表博客了,今天就發(fā)表篇博客給大家?guī)?lái)一些干貨,切勿錯(cuò)過(guò)哦。今天所介紹的主題是關(guān)于動(dòng)畫(huà)的,在之前的博客中也有用到動(dòng)畫(huà)的地方,今天就好好的總結(jié)一下iOS開(kāi)發(fā)中常用的動(dòng)畫(huà)。說(shuō)道動(dòng)畫(huà)其中有一個(gè)是仿射變換的概念,至于怎么仿射的怎么變換的,原理如何等在本篇博客中不做贅述。今天要分享的是如和用動(dòng)畫(huà)做出我們要做的效果。
今天主要用到的動(dòng)畫(huà)類是CALayer下的CATransition至于各種動(dòng)畫(huà)類中如何繼承的在這也不做贅述,網(wǎng)上的資料是一抓一大把。好廢話少說(shuō)切入今天的正題。
一.封裝動(dòng)畫(huà)方法
1.用CATransition實(shí)現(xiàn)動(dòng)畫(huà)的封裝方法如下,每句代碼是何意思,請(qǐng)看注釋之。
1 #PRagma CATransition動(dòng)畫(huà)實(shí)現(xiàn) 2 - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view 3 { 4 //創(chuàng)建CATransition對(duì)象 5 CATransition *animation = [CATransition animation]; 6 7 //設(shè)置運(yùn)動(dòng)時(shí)間 8 animation.duration = DURATION; 9 10 //設(shè)置運(yùn)動(dòng)type11 animation.type = type;12 if (subtype != nil) {13 14 //設(shè)置子類15 animation.subtype = subtype;16 }17 18 //設(shè)置運(yùn)動(dòng)速度19 animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;20 21 [view.layer addAnimation:animation forKey:@"animation"];22 }
代碼說(shuō)明:
CATransition常用的屬性如下:
duration:設(shè)置動(dòng)畫(huà)時(shí)間
type:稍后下面會(huì)詳細(xì)的介紹運(yùn)動(dòng)類型
subtype:和type匹配使用,指定運(yùn)動(dòng)的方向,下面也會(huì)詳細(xì)介紹
timingFunction :動(dòng)畫(huà)的運(yùn)動(dòng)軌跡,用于變化起點(diǎn)和終點(diǎn)之間的插值計(jì)算,形象點(diǎn)說(shuō)它決定了動(dòng)畫(huà)運(yùn)行的節(jié)奏,比如是
均勻變化(相同時(shí)間變化量相同)還是先快后慢,先慢后快還是先慢再快再慢.
* 動(dòng)畫(huà)的開(kāi)始與結(jié)束的快慢,有五個(gè)預(yù)置分別為(下同):
* kCAMediaTimingFunctionLinear 線性,即勻速
* kCAMediaTimingFunctionEaseIn 先慢后快
* kCAMediaTimingFunctionEaSEOut 先快后慢
* kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
* kCAMediaTimingFunctionDefault 實(shí)際效果是動(dòng)畫(huà)中間比較快.
2.用UIView的block回調(diào)實(shí)現(xiàn)動(dòng)畫(huà)的代碼封裝
1 #pragma UIView實(shí)現(xiàn)動(dòng)畫(huà)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的背景圖,便于切換時(shí)觀察
1 #pragma 給View添加背景圖2 -(void)addBgImageWithImageName:(NSString *) imageName3 {4 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];5 }
二.調(diào)用上面的方法實(shí)現(xiàn)我們想要的動(dòng)畫(huà)
1.我們?cè)赩iew上添加多個(gè)Button,給不同的Button設(shè)置不同的Tag值,然后再ViewController中綁定同一個(gè)方法,點(diǎn)擊不同的button實(shí)現(xiàn)不同的頁(yè)面切換效果。storyBoard上的控件效果如下圖所示:

2.下面我們就開(kāi)始編寫(xiě)點(diǎn)擊button要回調(diào)的方法
(1).定義枚舉來(lái)標(biāo)示按鈕所對(duì)應(yīng)的動(dòng)畫(huà)類型,代碼如下:
1 typedef enum : NSUInteger { 2 Fade = 1, //淡入淡出 3 Push, //推擠 4 Reveal, //揭開(kāi) 5 MoveIn, //覆蓋 6 Cube, //立方體 7 SuckEffect, //吮吸 8 OglFlip, //翻轉(zhuǎn) 9 RippleEffect, //波紋10 PageCurl, //翻頁(yè)11 PageUnCurl, //反翻頁(yè)12 CameraIrisHollowOpen, //開(kāi)鏡頭13 CameraIrisHollowClose, //關(guān)鏡頭14 CurlDown, //下翻頁(yè)15 CurlUp, //上翻頁(yè)16 FlipFromLeft, //左翻轉(zhuǎn)17 FlipFromRight, //右翻轉(zhuǎn)18 19 } AnimationType;
(2),獲取Button的Tag值:
1 UIButton *button = sender;2 AnimationType animationType = button.tag;
(3).每次點(diǎn)擊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),通過(guò)switch結(jié)合上邊的枚舉來(lái)判斷是那個(gè)按鈕點(diǎn)擊的
1 switch (animationType)2 {3 //各種Case,此處代碼下面會(huì)給出 4 }
3.調(diào)用我們封裝的運(yùn)動(dòng)方法,來(lái)實(shí)現(xiàn)動(dòng)畫(huà)效果
(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).揭開(kāi)效果:
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).翻轉(zhuǎn)效果
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).翻頁(yè)和反翻頁(yè)效果
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).相機(jī)打開(kāi)效果
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),調(diào)用上面封裝的第二個(gè)動(dòng)畫(huà)方法
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
今天的博客就先到這里吧,以后有時(shí)間還會(huì)更新內(nèi)容的,敬請(qǐng)關(guān)注哦!!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注