iOS開發(fā)中常用的各種動畫、頁面切面效果
今天主要用到的動畫類是CALayer下的CATransition至于各種動畫類中如何繼承的在這也不做贅述,網(wǎng)上的資料是一抓一大把。好廢話少說切入今天的正題。
一.封裝動畫方法
1.用CATransition實現(xiàn)動畫的封裝方法如下,每句代碼是何意思,請看注釋之。
#pragma CATransition動畫實現(xiàn) - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIView *) view { //創(chuàng)建CATransition對象 CATransition *animation = [CATransition animation]; //設(shè)置運動時間 animation.duration = DURATION; //設(shè)置運動type animation.type = type; if (subtype != nil) { //設(shè)置子類 animation.subtype = subtype; } //設(shè)置運動速度 animation.timingFunction = UIViewAnimationOptionCurveEaseInOut; [view.layer addAnimation:animation forKey:@"animation"]; }
代碼說明:
CATransition常用的屬性如下:
duration:設(shè)置動畫時間
type:稍后下面會詳細的介紹運動類型
subtype:和type匹配使用,指定運動的方向,下面也會詳細介紹
timingFunction :動畫的運動軌跡,用于變化起點和終點之間的插值計算,形象點說它決定了動畫運行的節(jié)奏,比如是
均勻變化(相同時間變化量相同)還是先快后慢,先慢后快還是先慢再快再慢.
* 動畫的開始與結(jié)束的快慢,有五個預(yù)置分別為(下同):
* kCAMediaTimingFunctionLinear 線性,即勻速
* kCAMediaTimingFunctionEaseIn 先慢后快
* kCAMediaTimingFunctionEaseOut 先快后慢
* kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
* kCAMediaTimingFunctionDefault 實際效果是動畫中間比較快.
2.用UIView的block回調(diào)實現(xiàn)動畫的代碼封裝
#pragma UIView實現(xiàn)動畫 - (void) animationWithView : (UIView *)view WithAnimationTransition : (UIViewAnimationTransition) transition { [UIView animateWithDuration:DURATION animations:^{ [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:transition forView:view cache:YES]; }]; }
3.改變View的背景圖,便于切換時觀察
#pragma 給View添加背景圖 -(void)addBgImageWithImageName:(NSString *) imageName { self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]]; }
二.調(diào)用上面的方法實現(xiàn)我們想要的動畫
1.我們在View上添加多個Button,給不同的Button設(shè)置不同的Tag值,然后再ViewController中綁定同一個方法,點擊不同的button實現(xiàn)不同的頁面切換效果。storyBoard上的控件效果如下圖所示:
2.下面我們就開始編寫點擊button要回調(diào)的方法
(1).定義枚舉來標示按鈕所對應(yīng)的動畫類型,代碼如下:
typedef enum : NSUInteger { Fade = , //淡入淡出 Push, //推擠 Reveal, //揭開 MoveIn, //覆蓋 Cube, //立方體 SuckEffect, //吮吸 OglFlip, //翻轉(zhuǎn) RippleEffect, //波紋 PageCurl, //翻頁 PageUnCurl, //反翻頁 CameraIrisHollowOpen, //開鏡頭 CameraIrisHollowClose, //關(guān)鏡頭 CurlDown, //下翻頁 CurlUp, //上翻頁 FlipFromLeft, //左翻轉(zhuǎn) FlipFromRight, //右翻轉(zhuǎn) } AnimationType;
(2),獲取Button的Tag值:
UIButton *button = sender; AnimationType animationType = button.tag;
(3).每次點擊button都改變subtype的值,包括上,左,下,右
NSString *subtypeString; switch (_subtype) { case : subtypeString = kCATransitionFromLeft; break; case : subtypeString = kCATransitionFromBottom; break; case : subtypeString = kCATransitionFromRight; break; case : subtypeString = kCATransitionFromTop; break; default: break; } _subtype += ; if (_subtype > ) { _subtype = ; }
(4),通過switch結(jié)合上邊的枚舉來判斷是那個按鈕點擊的
switch (animationType) { //各種Case,此處代碼下面會給出 }
3.調(diào)用我們封裝的運動方法,來實現(xiàn)動畫效果
(1),淡化效果
case Fade: [self transitionWithType:kCATransitionFade WithSubtype:subtypeString ForView:self.view]; break;
(2).Push效果
case Push: [self transitionWithType:kCATransitionPush WithSubtype:subtypeString ForView:self.view]; break;
效果如下:
(3).揭開效果:
case Reveal: [self transitionWithType:kCATransitionReveal WithSubtype:subtypeString ForView:self.view]; break;
效果圖如下:
(4).覆蓋效果
case MoveIn: [self transitionWithType:kCATransitionMoveIn WithSubtype:subtypeString ForView:self.view]; break;
效果圖如下:
(5).立方體效果
case Cube: [self transitionWithType:@"cube" WithSubtype:subtypeString ForView:self.view]; break;
效果如下:
(6).吮吸效果
case SuckEffect: [self transitionWithType:@"suckEffect" WithSubtype:subtypeString ForView:self.view]; break;
效果如下:
(7).翻轉(zhuǎn)效果
case OglFlip: [self transitionWithType:@"oglFlip" WithSubtype:subtypeString ForView:self.view]; break;
效果圖如下:
8.波紋效果
case RippleEffect: [self transitionWithType:@"rippleEffect" WithSubtype:subtypeString ForView:self.view]; break;
(9).翻頁和反翻頁效果
case PageCurl: [self transitionWithType:@"pageCurl" WithSubtype:subtypeString ForView:self.view]; break; case PageUnCurl: [self transitionWithType:@"pageUnCurl" WithSubtype:subtypeString ForView:self.view]; break;
(10).相機打開效果
case CameraIrisHollowOpen: [self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:subtypeString ForView:self.view]; break; case CameraIrisHollowClose: [self transitionWithType:@"cameraIrisHollowClose" WithSubtype:subtypeString ForView:self.view]; break;
(11),調(diào)用上面封裝的第二個動畫方法
case CurlDown: [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlDown]; break; case CurlUp: [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionCurlUp]; break; case FlipFromLeft: [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromLeft]; break; case FlipFromRight: [self animationWithView:self.view WithAnimationTransition:UIViewAnimationTransitionFlipFromRight]; break;
以上內(nèi)容是針對iOS開發(fā)中常用的各種動畫、頁面切面效果的相關(guān)介紹,希望對大家有所幫助!
- iOS 9 更新之Safari廣告攔截器(Content Blocker)開發(fā)教程
- iOS中Block的回調(diào)使用和解析詳解
- 匯總ios開發(fā)逆向傳值的方法
- iOS開發(fā)中ViewController的頁面跳轉(zhuǎn)和彈出模態(tài)
- iOS應(yīng)用開發(fā)中實現(xiàn)頁面跳轉(zhuǎn)的簡單方法筆記
- iOS開發(fā)之路--微博新特性頁面
- iOS開發(fā)之路--微博“更多”頁面
- IOS開發(fā)向右滑動返回前一個頁面功能(demo)
- 如何使用jQuery技術(shù)開發(fā)ios風格的頁面導航菜單
- iOS通過block在兩個頁面間傳值的方法
相關(guān)文章
iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ)
這篇文章主要介紹了iOS系統(tǒng)緩存方面開發(fā)的相關(guān)基礎(chǔ),示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10IOS開發(fā) UIAlertController詳解及實例代碼
這篇文章主要介紹了 IOS開發(fā) UIAlertController詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12iOS利用Runtime實現(xiàn)友盟頁面數(shù)據(jù)統(tǒng)計的功能示例
這篇文章主要給大家介紹了關(guān)于iOS利用Runtime實現(xiàn)友盟頁面數(shù)據(jù)統(tǒng)計功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01iOS手勢識別的詳細使用方法(拖動,縮放,旋轉(zhuǎn),點擊,手勢依賴,自定義手勢)
這篇文章主要介紹了iOS手勢識別的詳細使用方法(拖動,縮放,旋轉(zhuǎn),點擊,手勢依賴,自定義手勢),具有一定的參考價值,有需要的可以參考一下。2016-11-11