UIView動畫UIView Animation總結(jié)

UIView動畫UIView Animation總結(jié) 原文地址

基本動畫

最常用的幾種方式

//duration動畫持續(xù)時間, animations想要完成動畫block, UIView動畫完成時的最終狀態(tài)
+ (void)animateWithDuration:(NSTimeInterval)duration 
                 animations:(void (^)(void))animations

//completion 動畫完成block, BOOL finished 表示動畫是否完成
+ (void)animateWithDuration:(NSTimeInterval)duration 
                 animations:(void (^)(void))animations 
                 completion:(void (^ __nullable)(BOOL finished))completion

//delay 延遲執(zhí)行時間, options 動畫效果枚舉
+ (void)animateWithDuration:(NSTimeInterval)duration
                      delay:(NSTimeInterval)delay 
                      options:(UIViewAnimationOptions)options 
                      animations:(void (^)(void))animations 
                      completion:(void (^ __nullable)(BOOL finished))completion

animations 修改View屬性的Block 下面是支持修改的屬性

@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch

options動畫效果枚舉 相關(guān)

UIViewAnimationOptionLayoutSubviews            //提交動畫的時候布局子控件春贸,表示子控件將和父控件一同動畫妇斤。
UIViewAnimationOptionAllowUserInteraction      //動畫時允許用戶交流苫费,比如觸摸
UIViewAnimationOptionBeginFromCurrentState     //從當(dāng)前狀態(tài)開始動畫
UIViewAnimationOptionRepeat                    //動畫無限重復(fù)
UIViewAnimationOptionAutoreverse               //執(zhí)行動畫回路,前提是設(shè)置動畫無限重復(fù)
UIViewAnimationOptionOverrideInheritedDuration //忽略外層動畫嵌套的執(zhí)行時間
UIViewAnimationOptionOverrideInheritedCurve    //忽略外層動畫嵌套的時間變化曲線
UIViewAnimationOptionAllowAnimatedContent      //通過改變屬性和重繪實現(xiàn)動畫效果,如果key沒有提交動畫將使用快照
UIViewAnimationOptionShowHideTransitionViews   //用顯隱的方式替代添加移除圖層的動畫效果
UIViewAnimationOptionOverrideInheritedOptions  //忽略嵌套繼承的選項

時間函數(shù)曲線相關(guān)

UIViewAnimationOptionCurveEaseInOut            //時間曲線函數(shù)黍析,由慢到快
UIViewAnimationOptionCurveEaseIn               //時間曲線函數(shù)三妈,由慢到特別快
UIViewAnimationOptionCurveEaseOut              //時間曲線函數(shù),由快到慢
UIViewAnimationOptionCurveLinear               //時間曲線函數(shù)熙掺,勻速

轉(zhuǎn)場動畫相關(guān)的

UIViewAnimationOptionTransitionNone            //無轉(zhuǎn)場動畫
UIViewAnimationOptionTransitionFlipFromLeft    //轉(zhuǎn)場從左翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromRight   //轉(zhuǎn)場從右翻轉(zhuǎn)
UIViewAnimationOptionTransitionCurlUp          //上卷轉(zhuǎn)場
UIViewAnimationOptionTransitionCurlDown        //下卷轉(zhuǎn)場
UIViewAnimationOptionTransitionCrossDissolve   //轉(zhuǎn)場交叉消失
UIViewAnimationOptionTransitionFlipFromTop     //轉(zhuǎn)場從上翻轉(zhuǎn)
UIViewAnimationOptionTransitionFlipFromBottom  //轉(zhuǎn)場從下翻轉(zhuǎn)

彈簧動畫

+ (void)animateWithDuration:(NSTimeInterval)duration 
                      delay:(NSTimeInterval)delay 
                      usingSpringWithDamping:(CGFloat)dampingRatio 
                      initialSpringVelocity:(CGFloat)velocity 
                      options:(UIViewAnimationOptions)options
                      animations:(void (^)(void))animations 
                      completion:(void (^ __nullable)(BOOL finished))completion

dampingRatio 彈簧的阻尼 如果為1動畫則平穩(wěn)減速動畫沒有振蕩谤碳。 這里值為 0~1
velocity 彈簧的速率溃卡。數(shù)值越小,動力越小蜒简,彈簧的拉伸幅度就越小瘸羡。反之相反。比如:總共的動畫運行距離是200 pt搓茬,你希望每秒 100pt 時犹赖,值為 0.5;
PS:

[UIView animateWithDuration:2
                      delay:2
     usingSpringWithDamping:.5
      initialSpringVelocity:.5
                    options:UIViewAnimationOptionRepeat
                animations:^{
   view.center = self.view.center;
} completion:^(BOOL finished) {   
}];
彈簧動畫

過渡動畫

//view 參與轉(zhuǎn)換的視圖
+ (void)transitionWithView:(UIView *)view 
                  duration:(NSTimeInterval)duration 
                  options:(UIViewAnimationOptions)options
                  animations:(void (^ __nullable)(void))animations 
                  completion:(void (^ __nullable)(BOOL finished))completion

PS:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.center = self.view.center;
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

UIView *view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
view_1.backgroundColor = [UIColor yellowColor];
[UIView transitionWithView:view 
                  duration:3      
                  options:UIViewAnimationOptionTransitionCurlUp 
                  animations:^{
                  [view addSubview:view_1];      
} completion:^(BOOL finished) {        
}];
過渡動畫1
//fromView 一開始的視圖, toView 轉(zhuǎn)換后的視圖
+ (void)transitionFromView:(UIView *)fromView
                    toView:(UIView *)toView 
                    duration:(NSTimeInterval)duration 
                    options:(UIViewAnimationOptions)options 
                    completion:(void (^ __nullable)(BOOL finished))completion

PS:

UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
baseView.center = self.view.center;
[self.view addSubview:baseView];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[baseView addSubview:view];

UIView *view_1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view_1.backgroundColor = [UIColor yellowColor];

[UIView transitionFromView:view
                    toView:view_1 
                  duration:2 
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:^(BOOL finished) {    
}];
過渡動畫2

關(guān)鍵幀動畫

/**
*  @param duration 總持續(xù)時間
*  @param UIViewKeyframeAnimationOptions options 枚舉 下面說明
*  @param frameStartTime 相對開始時間
*  @param frameDuration 相對持續(xù)時間
*/
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
                              delay:(NSTimeInterval)delay 
                              options:(UIViewKeyframeAnimationOptions)options 
                              animations:(void (^)(void))animations
                               completion:(void (^ __nullable)(BOOL finished))completion
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime 
                        relativeDuration:(double)frameDuration 
                        animations:(void (^)(void))animations

PS:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];

[UIView animateKeyframesWithDuration:3 delay:3 options:UIViewKeyframeAnimationOptionRepeat animations:^{
    [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:.3 animations:^{
            view.frame = CGRectMake(20, 100, 100, 100);
    }];
     [UIView addKeyframeWithRelativeStartTime:.3 relativeDuration:.6 animations:^{
            view.frame = CGRectMake(60, 100, 80, 80);
    }];
    [UIView addKeyframeWithRelativeStartTime:.6 relativeDuration:1 animations:^{
            view.frame = CGRectMake(20, 20, 50, 50);
    }];
} completion:^(BOOL finished) {
}];
關(guān)鍵幀動畫

UIViewKeyframeAnimationOptions

UIViewKeyframeAnimationOptionLayoutSubviews        //提交動畫的時候布局子控件卷仑,表示子控件將和父控件一同動畫峻村。
UIViewKeyframeAnimationOptionAllowUserInteraction  //動畫時允許用戶交流,比如觸摸    UIViewKeyframeAnimationOptionBeginFromCurrentState     //從當(dāng)前狀態(tài)開始動畫
UIViewKeyframeAnimationOptionRepeat                //動畫無限重復(fù)
UIViewKeyframeAnimationOptionAutoreverse           //執(zhí)行動畫回路,前提是設(shè)置動畫無限重復(fù)
UIViewKeyframeAnimationOptionOverrideInheritedDuration //忽略外層動畫嵌套的執(zhí)行時間
UIViewKeyframeAnimationOptionOverrideInheritedOptions  //忽略嵌套繼承的選項

關(guān)鍵幀動畫獨有

UIViewKeyframeAnimationOptionCalculationModeLinear     //選擇使用一個簡單的線性插值計算的時候關(guān)鍵幀之間的值锡凝。
UIViewKeyframeAnimationOptionCalculationModeDiscrete   //選擇不插入關(guān)鍵幀之間的值,而是直接跳到每個新的關(guān)鍵幀的值粘昨。
UIViewKeyframeAnimationOptionCalculationModePaced      //選擇計算中間幀數(shù)值算法使用一個簡單的節(jié)奏。這個選項的結(jié)果在一個均勻的動畫。
UIViewKeyframeAnimationOptionCalculationModeCubic      //選擇計算中間幀使用默認(rèn)卡特莫爾羅花鍵,通過關(guān)鍵幀的值张肾。你不能調(diào)整該算法的參數(shù)芭析。 這個動畫好像會更圓滑一些..
UIViewKeyframeAnimationOptionCalculationModeCubicPaced //選擇計算中間幀使用立方計劃而忽略的時間屬性動畫。相反,時間參數(shù)計算隱式地給動畫一個恒定的速度吞瞪。

最后兩個

+ (void)performSystemAnimation:(UISystemAnimation)animation 
                       onViews:(NSArray *)views
                       options:(UIViewAnimationOptions)options 
                       animations:(void (^ __nullable)(void))parallelAnimations completion:(void (^ __nullable)(BOOL finished))completion
+ (void)performWithoutAnimation:(void (NS_NOESCAPE ^)(void))actionsWithoutAnimation

1.刪除視圖上的子視圖 animation這個枚舉只有一個刪除值...
views操作的view 這會讓那個視圖變模糊馁启、收縮和褪色, 之后再給它發(fā)送 removeFromSuperview 方法。
2.在動畫block中不執(zhí)行動畫的代碼.
PS:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];

UIView *view_1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
view_1.backgroundColor = [UIColor redColor];
[view addSubview:view_1];

[UIView animateKeyframesWithDuration:3 delay:3 options:UIViewKeyframeAnimationOptionRepeat|UIViewKeyframeAnimationOptionAutoreverse animations:^{
        view.frame = CGRectMake(100, 100, 50, 50);
        [UIView performWithoutAnimation:^{
            view.backgroundColor = [UIColor blueColor];
        }];
    } completion:^(BOOL finished) {
    }];
    [UIView performSystemAnimation:UISystemAnimationDelete 
                           onViews:@[view_1] 
                           options:0 
                           animations:^{
        view_1.alpha = 0;
    } completion:^(BOOL finished) {
    }];
image
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末芍秆,一起剝皮案震驚了整個濱河市惯疙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌浪听,老刑警劉巖螟碎,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異迹栓,居然都是意外死亡,警方通過查閱死者的電腦和手機俭缓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門克伊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人华坦,你說我怎么就攤上這事愿吹。” “怎么了惜姐?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵犁跪,是天一觀的道長。 經(jīng)常有香客問我歹袁,道長坷衍,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任条舔,我火速辦了婚禮枫耳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘孟抗。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布旦委。 她就那樣靜靜地躺著匠楚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摊沉。 梳的紋絲不亂的頭發(fā)上狐史,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音,去河邊找鬼预皇。 笑死侈玄,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的吟温。 我是一名探鬼主播序仙,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鲁豪!你這毒婦竟也來了潘悼?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤爬橡,失蹤者是張志新(化名)和其女友劉穎治唤,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糙申,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡宾添,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了柜裸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缕陕。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖疙挺,靈堂內(nèi)的尸體忽然破棺而出扛邑,到底是詐尸還是另有隱情,我是刑警寧澤铐然,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布蔬崩,位于F島的核電站,受9級特大地震影響搀暑,放射性物質(zhì)發(fā)生泄漏沥阳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一险掀、第九天 我趴在偏房一處隱蔽的房頂上張望沪袭。 院中可真熱鬧,春花似錦樟氢、人聲如沸冈绊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽死宣。三九已至,卻和暖如春碴开,著一層夾襖步出監(jiān)牢的瞬間毅该,已是汗流浹背博秫。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留眶掌,地道東北人挡育。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像朴爬,于是被迫代替她去往敵國和親即寒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 在iOS實際開發(fā)中常用的動畫無非是以下四種:UIView動畫召噩,核心動畫母赵,幀動畫,自定義轉(zhuǎn)場動畫具滴。 1.UIView...
    請叫我周小帥閱讀 3,097評論 1 23
  • 重點參考鏈接: View Programming Guide for iOS https://developer....
    Kevin_Junbaozi閱讀 4,438評論 0 15
  • 在iOS中隨處都可以看到絢麗的動畫效果凹嘲,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌构韵。在這里你可以看...
    每天刷兩次牙閱讀 8,489評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果周蹭,實現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌贞绳。在這里你可以看...
    F麥子閱讀 5,111評論 5 13
  • 薛之謙小哥哥又出了新歌了谷醉,還是同樣的調(diào)調(diào),同樣的憂傷冈闭,同樣的哀而不傷,我很喜歡這種感覺抖单。 我一直覺得愛情來的有點快...
    腹黑小蘋果閱讀 820評論 0 1