iOS動畫篇:UIView動畫

iOS的動畫效果一直都很棒很科贬,給人的感覺就是很炫酷很流暢,起到增強用戶體驗的作用羔飞。在APP開發(fā)中實現(xiàn)動畫效果有很多種方式佣盒,對于簡單的應用場景关带,我們可以使用UIKit提供的動畫來實現(xiàn)。

UIView動畫簡介

UIView動畫實質上是對Core Animation的封裝沼撕,提供簡潔的動畫接口。

UIView動畫可以設置的動畫屬性有:
1芜飘、大小變化(frame)
2务豺、拉伸變化(bounds)
3、中心位置(center)
4嗦明、旋轉(transform)
5笼沥、透明度(alpha)
6、背景顏色(backgroundColor)
7娶牌、拉伸內容(contentStretch)

UIview 類方法動畫

1)動畫的開始和結束方法

1.1 動畫開始標記

[UIView beginAnimations:(nullable NSString *) context:(nullable void *)];

第一個參數(shù):動畫標識
第二個參數(shù):附加參數(shù)奔浅,在設置了代理的情況下,此參數(shù)將發(fā)送到setAnimationWillStartSelector和setAnimationDidStopSelector所指定的方法诗良。大部分情況下汹桦,我們設置為nil即可。

1.2 結束動畫標記

    [UIView commitAnimations];
2)動畫參數(shù)的設置方法
    //動畫持續(xù)時間
    [UIView setAnimationDuration:(NSTimeInterval)];
    //動畫的代理對象
    [UIView setAnimationDelegate:(nullable id)];
    //設置動畫將開始時代理對象執(zhí)行的SEL
    [UIView setAnimationWillStartSelector:(nullable SEL)];
    //設置動畫結束時代理對象執(zhí)行的SEL
    [UIView setAnimationDidStopSelector:(nullable SEL)];
    //設置動畫延遲執(zhí)行的時間
    [UIView setAnimationDelay:(NSTimeInterval)];
    //設置動畫的重復次數(shù)
    [UIView setAnimationRepeatCount:(float)];
    //設置動畫的曲線
    [UIView setAnimationCurve:(UIViewAnimationCurve)];
    UIViewAnimationCurve的枚舉值如下:
    UIViewAnimationCurveEaseInOut,         // 慢進慢出(默認值)
    UIViewAnimationCurveEaseIn,            // 慢進
    UIViewAnimationCurveEaseOut,           // 慢出
    UIViewAnimationCurveLinear             // 勻速
    //設置是否從當前狀態(tài)開始播放動畫
    [UIView setAnimationBeginsFromCurrentState:YES];
    假設上一個動畫正在播放鉴裹,且尚未播放完畢舞骆,我們將要進行一個新的動畫:
    當為YES時:動畫將從上一個動畫所在的狀態(tài)開始播放
    當為NO時:動畫將從上一個動畫所指定的最終狀態(tài)開始播放(此時上一個動畫馬上結束)
    //設置動畫是否繼續(xù)執(zhí)行相反的動畫
    [UIView setAnimationRepeatAutoreverses:(BOOL)];
    //是否禁用動畫效果(對象屬性依然會被改變钥弯,只是沒有動畫效果)
    [UIView setAnimationsEnabled:(BOOL)];
    //設置視圖的過渡效果
    [UIView setAnimationTransition:(UIViewAnimationTransition) forView:(nonnull UIView *) cache:(BOOL)];
     第一個參數(shù):UIViewAnimationTransition的枚舉值如下
         UIViewAnimationTransitionNone,              //不使用動畫
         UIViewAnimationTransitionFlipFromLeft,      //從左向右旋轉翻頁
         UIViewAnimationTransitionFlipFromRight,     //從右向左旋轉翻頁
         UIViewAnimationTransitionCurlUp,            //從下往上卷曲翻頁
         UIViewAnimationTransitionCurlDown,          //從上往下卷曲翻頁
     第二個參數(shù):需要過渡效果的View
     第三個參數(shù):是否使用視圖緩存,YES:視圖在開始和結束時渲染一次督禽;NO:視圖在每一幀都渲染
3)實例代碼:

1脆霎、屬性變化動畫(以frame變化為例)

- (void)changeFrame {
    [UIView beginAnimations:@"FrameAni" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    self.cartCenter.frame = self.centerShow.frame;
    [UIView commitAnimations];
}

- (void)startAni:(NSString *)aniID {
    NSLog(@"%@ start",aniID);
}

- (void)stopAni:(NSString *)aniID {
    NSLog(@"%@ stop",aniID);
}

動畫效果:

NormalAni.gif

2、轉場效果動畫(以Flip效果為例)

- (void)flipAni {
    [UIView beginAnimations:@"FlipAni" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.centerShow cache:YES];
    self.centerShow.image = [UIImage imageNamed:@"service"];
    [UIView commitAnimations];
}

動畫效果:

ScreenTransitionAni.gif

UIview Block動畫

iOS4.0以后狈惫,增加了Block動畫塊睛蛛,提供更簡潔的方式來實現(xiàn)動畫。

1)Block動畫方法

1胧谈、最簡潔的Block動畫:包含時間和動畫

 [UIView animateWithDuration:(NSTimeInterval)  //動畫持續(xù)時間
                  animations:^{
                  //執(zhí)行的動畫
 }];

2忆肾、帶有動畫完成回調的Block動畫

 [UIView animateWithDuration:(NSTimeInterval)  //動畫持續(xù)時間
                  animations:^{
                //執(zhí)行的動畫
 }                completion:^(BOOL finished) {
                //動畫執(zhí)行完畢后的操作
 }];

3、可設置延遲時間和過渡效果的Block動畫

 [UIView animateWithDuration:(NSTimeInterval) //動畫持續(xù)時間
                       delay:(NSTimeInterval) //動畫延遲執(zhí)行的時間
                     options:(UIViewAnimationOptions) //動畫的過渡效果
                  animations:^{
                   //執(zhí)行的動畫
 }                completion:^(BOOL finished) {
                   //動畫執(zhí)行完畢后的操作
 }];

UIViewAnimationOptions的枚舉值如下第岖,可組合使用:

 UIViewAnimationOptionLayoutSubviews            //進行動畫時布局子控件
 UIViewAnimationOptionAllowUserInteraction      //進行動畫時允許用戶交互
 UIViewAnimationOptionBeginFromCurrentState     //從當前狀態(tài)開始動畫
 UIViewAnimationOptionRepeat                    //無限重復執(zhí)行動畫
 UIViewAnimationOptionAutoreverse               //執(zhí)行動畫回路
 UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套動畫的執(zhí)行時間設置
 UIViewAnimationOptionOverrideInheritedCurve    //忽略嵌套動畫的曲線設置
 UIViewAnimationOptionAllowAnimatedContent      //轉場:進行動畫時重繪視圖
 UIViewAnimationOptionShowHideTransitionViews   //轉場:移除(添加和移除圖層的)動畫效果
 UIViewAnimationOptionOverrideInheritedOptions  //不繼承父動畫設置
 
 UIViewAnimationOptionCurveEaseInOut            //時間曲線难菌,慢進慢出(默認值)
 UIViewAnimationOptionCurveEaseIn               //時間曲線,慢進
 UIViewAnimationOptionCurveEaseOut              //時間曲線蔑滓,慢出
 UIViewAnimationOptionCurveLinear               //時間曲線郊酒,勻速
 
 UIViewAnimationOptionTransitionNone            //轉場,不使用動畫
 UIViewAnimationOptionTransitionFlipFromLeft    //轉場键袱,從左向右旋轉翻頁
 UIViewAnimationOptionTransitionFlipFromRight   //轉場燎窘,從右向左旋轉翻頁
 UIViewAnimationOptionTransitionCurlUp          //轉場,下往上卷曲翻頁
 UIViewAnimationOptionTransitionCurlDown        //轉場蹄咖,從上往下卷曲翻頁
 UIViewAnimationOptionTransitionCrossDissolve   //轉場褐健,交叉消失和出現(xiàn)
 UIViewAnimationOptionTransitionFlipFromTop     //轉場,從上向下旋轉翻頁
 UIViewAnimationOptionTransitionFlipFromBottom  //轉場澜汤,從下向上旋轉翻頁

4蚜迅、Spring動畫
iOS7.0后新增Spring動畫(iOS系統(tǒng)動畫大部分采用Spring Animation,適用于所有可被添加動畫效果的屬性)

 [UIView animateWithDuration:(NSTimeInterval)//動畫持續(xù)時間
                       delay:(NSTimeInterval)//動畫延遲執(zhí)行的時間
      usingSpringWithDamping:(CGFloat)//震動效果俊抵,范圍0~1谁不,數(shù)值越小震動效果越明顯
       initialSpringVelocity:(CGFloat)//初始速度,數(shù)值越大初始速度越快
                     options:(UIViewAnimationOptions)//動畫的過渡效果
                  animations:^{
                     //執(zhí)行的動畫
 }
                  completion:^(BOOL finished) {
                     //動畫執(zhí)行完畢后的操作
 }];

5徽诲、Keyframes動畫
iOS7.0后新增關鍵幀動畫刹帕,支持屬性關鍵幀,不支持路徑關鍵幀

 [UIView animateKeyframesWithDuration:(NSTimeInterval)//動畫持續(xù)時間
                                delay:(NSTimeInterval)//動畫延遲執(zhí)行的時間
                              options:(UIViewKeyframeAnimationOptions)//動畫的過渡效果
                           animations:^{
                         //執(zhí)行的關鍵幀動畫
 }
                           completion:^(BOOL finished) {
                         //動畫執(zhí)行完畢后的操作
 }];

UIViewKeyframeAnimationOptions的枚舉值如下谎替,可組合使用:

UIViewAnimationOptionLayoutSubviews           //進行動畫時布局子控件
UIViewAnimationOptionAllowUserInteraction     //進行動畫時允許用戶交互
UIViewAnimationOptionBeginFromCurrentState    //從當前狀態(tài)開始動畫
UIViewAnimationOptionRepeat                   //無限重復執(zhí)行動畫
UIViewAnimationOptionAutoreverse              //執(zhí)行動畫回路
UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套動畫的執(zhí)行時間設置
UIViewAnimationOptionOverrideInheritedOptions //不繼承父動畫設置

UIViewKeyframeAnimationOptionCalculationModeLinear     //運算模式 :連續(xù)
UIViewKeyframeAnimationOptionCalculationModeDiscrete   //運算模式 :離散
UIViewKeyframeAnimationOptionCalculationModePaced      //運算模式 :均勻執(zhí)行
UIViewKeyframeAnimationOptionCalculationModeCubic      //運算模式 :平滑
UIViewKeyframeAnimationOptionCalculationModeCubicPaced //運算模式 :平滑均勻

各種運算模式的直觀比較如下圖:


圖片來源網絡.png

增加關鍵幀的方法:

 [UIView addKeyframeWithRelativeStartTime:(double)//動畫開始的時間(占總時間的比例)
                         relativeDuration:(double) //動畫持續(xù)時間(占總時間的比例)
                               animations:^{
                             //執(zhí)行的動畫
 }];

6偷溺、轉場動畫
6.1 從舊視圖轉到新視圖的動畫效果

 [UIView transitionFromView:(nonnull UIView *)
                     toView:(nonnull UIView *)
                   duration:(NSTimeInterval)
                    options:(UIViewAnimationOptions)
                 completion:^(BOOL finished) {
                     //動畫執(zhí)行完畢后的操作
 }];

在該動畫過程中,fromView 會從父視圖中移除钱贯,并講 toView 添加到父視圖中挫掏,注意轉場動畫的作用對象是父視圖(過渡效果體現(xiàn)在父視圖上)。
調用該方法相當于執(zhí)行下面兩句代碼:

[fromView.superview addSubview:toView];
[fromView removeFromSuperview];

6.2 單個視圖的過渡效果

 [UIView transitionWithView:(nonnull UIView *)
                   duration:(NSTimeInterval)
                    options:(UIViewAnimationOptions)
                 animations:^{
                 //執(zhí)行的動畫
 }
                 completion:^(BOOL finished) {
                 //動畫執(zhí)行完畢后的操作
 }];
2)實例代碼:

1喷舀、普通動畫
下面三段代碼都實現(xiàn)了相同的視圖frame的變化砍濒,不同之處只在于其延遲時間淋肾、過渡效果和結束回調。

- (void)blockAni1 {
    [UIView animateWithDuration:1.0 animations:^{
        self.cartCenter.frame = self.centerShow.frame;
    }];
}
- (void)blockAni2 {
    [UIView animateWithDuration:1.0 animations:^{
        self.cartCenter.frame = self.centerShow.frame;
    } completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}
- (void)blockAni3 {
    [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.cartCenter.frame = self.centerShow.frame;
    } completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}

動畫效果:

NormalAni.gif

2爸邢、Spring動畫

- (void)blockAni4 {
    [UIView animateWithDuration:1.0 delay:0.f usingSpringWithDamping:0.5 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.cartCenter.frame = self.centerShow.frame;
    } completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}

動畫效果:

SpringAni.gif

3樊卓、Keyframes動畫
這里以實現(xiàn)視圖背景顏色變化(紅-綠-藍-紫)的過程來演示關鍵幀動畫。

- (void)blockAni5 {
    self.centerShow.image = nil;
    [UIView animateKeyframesWithDuration:9.0 delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.9475 green:0.1921 blue:0.1746 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.1064 green:0.6052 blue:0.0334 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.1366 green:0.3017 blue:0.8411 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.619 green:0.037 blue:0.6719 alpha:1.0];
        }];
    } completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}

動畫效果:

KeyFramesAni.gif

4杠河、轉場動畫
4.1 單個視圖的過渡效果

- (void)blockAni6 {
    [UIView transitionWithView:self.centerShow duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.centerShow.image = [UIImage imageNamed:@"Service"];
    } completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}

動畫效果:

TransitionAni.gif

4.2 從舊視圖轉到新視圖的動畫效果

- (void)blockAni7 {
    UIImageView * newCenterShow = [[UIImageView alloc]initWithFrame:self.centerShow.frame];
    newCenterShow.image = [UIImage imageNamed:@"Service"];
    [UIView transitionFromView:self.centerShow toView:newCenterShow duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
        NSLog(@"動畫結束");
    }];
}

動畫效果:

ScreenTransitionAni.gif

Next

接下來將更新核心動畫Core Animation

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末碌尔,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子券敌,更是在濱河造成了極大的恐慌唾戚,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件待诅,死亡現(xiàn)場離奇詭異叹坦,居然都是意外死亡,警方通過查閱死者的電腦和手機卑雁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門募书,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人测蹲,你說我怎么就攤上這事莹捡。” “怎么了扣甲?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵篮赢,是天一觀的道長。 經常有香客問我琉挖,道長启泣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任示辈,我火速辦了婚禮种远,結果婚禮上,老公的妹妹穿的比我還像新娘顽耳。我一直安慰自己,他們只是感情好妙同,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布射富。 她就那樣靜靜地躺著,像睡著了一般粥帚。 火紅的嫁衣襯著肌膚如雪胰耗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天芒涡,我揣著相機與錄音柴灯,去河邊找鬼卖漫。 笑死,一個胖子當著我的面吹牛赠群,可吹牛的內容都是我干的羊始。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼查描,長吁一口氣:“原來是場噩夢啊……” “哼突委!你這毒婦竟也來了?” 一聲冷哼從身側響起冬三,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤匀油,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后勾笆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體敌蚜,經...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年窝爪,在試婚紗的時候發(fā)現(xiàn)自己被綠了弛车。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡酸舍,死狀恐怖帅韧,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情啃勉,我是刑警寧澤忽舟,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站淮阐,受9級特大地震影響叮阅,放射性物質發(fā)生泄漏。R本人自食惡果不足惜泣特,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一浩姥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧状您,春花似錦勒叠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至柒桑,卻和暖如春弊决,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背魁淳。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工飘诗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留与倡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓昆稿,卻偏偏與公主長得像纺座,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子貌嫡,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內容

  • 在開發(fā)工作中比驻,我們會碰到大量的需要表現(xiàn)力的地方,那么動畫便成為了我們開發(fā)過程中必不可少的知識點岛抄。 那么别惦,首先我們從...
    穿山甲救蛇精閱讀 2,591評論 2 15
  • 在iOS實際開發(fā)中常用的動畫無非是以下四種:UIView動畫,核心動畫夫椭,幀動畫掸掸,自定義轉場動畫。 1.UIView...
    請叫我周小帥閱讀 3,101評論 1 23
  • 在iOS中隨處都可以看到絢麗的動畫效果蹭秋,實現(xiàn)這些動畫的過程并不復雜扰付,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,501評論 6 30
  • 概覽 在iOS中隨處都可以看到絢麗的動畫效果仁讨,實現(xiàn)這些動畫的過程并不復雜羽莺,今天將帶大家一窺iOS動畫全貌。在這里你...
    Yiart閱讀 3,819評論 3 34
  • 合理的分布章節(jié)洞豁,每個章節(jié)獨立而又連接緊密盐固。 給人設更多的選擇空間,精神才是人設的內涵丈挟。 架構上留下足夠豐富的二次創(chuàng)...
    等待三月腐草為螢閱讀 354評論 0 1