UIViewAnimation —— 使用注意點(diǎn)

注意點(diǎn):對(duì)于蘋果的頭文件的查看,重要的方法或?qū)傩詴?huì)放在最前面

一鳄厌、首尾式動(dòng)畫注意點(diǎn):

1、簡(jiǎn)單的提交動(dòng)畫
注意點(diǎn):這兩個(gè)是成對(duì)出現(xiàn)的
[UIView beginAnimations:nil context:nil];
 // 開始動(dòng)畫// Code...
[UIView commitAnimations]; // 提交動(dòng)畫
+ (void)beginAnimations:(nullable NSString *)animationID 
                context:(nullable void *)context;

animationID : 作為動(dòng)畫的標(biāo)識(shí)
context: 自定義的一些動(dòng)畫屬性妈踊,這些數(shù)據(jù)將發(fā)送給動(dòng)畫的代理方法:setAnimationWillStartSelector:方法和setAnimationDidStopSelector:方法了嚎。這個(gè),參數(shù)廊营,通常為nil歪泳。我們可以直接設(shè)置為nil。
我們使用UIGraphicsGetCurrentContext()露筒;因?yàn)榇朔椒J(rèn)也會(huì)返回nil呐伞。

該方法告訴系統(tǒng),我們將開始動(dòng)畫慎式。
并且伶氢,在該方法后,我們可以通過setAnimationXXX(一系列方法)來設(shè)置我們進(jìn)行的動(dòng)畫的一些參數(shù)瞬捕。
完成動(dòng)畫后鞍历,調(diào)用commitAnimations方法來通知系統(tǒng),動(dòng)畫結(jié)束肪虎。

如果我們?yōu)閯?dòng)畫設(shè)置了劣砍,setAnimationWillStartSelector:方法和setAnimationDidStopSelector:方法。
那么當(dāng)動(dòng)畫開始或者停止的時(shí)候扇救,動(dòng)畫的animationID參數(shù)和context參數(shù)刑枝,會(huì)傳遞給setAnimationWillStartSelector:方法和setAnimationDidStopSelector:方法香嗓。
// 獲取當(dāng)前的圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
2、下面動(dòng)畫 1 和 動(dòng)畫 2 是同步執(zhí)行的
/** 動(dòng)畫 1*/
[UIView beginAnimations:nil context:nil];
 // 開始動(dòng)畫// Code...
[UIView commitAnimations]; // 提交動(dòng)畫

/*** 動(dòng)畫 2*/
[UIView beginAnimations:nil context:nil];
 // 開始動(dòng)畫// Code...
[UIView commitAnimations]; // 提交動(dòng)畫
**setAnimationBeginsFromCurrentState****方法**
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState

設(shè)置動(dòng)畫開始時(shí)的狀態(tài)装畅。

我們構(gòu)想一個(gè)場(chǎng)景:一般靠娱,我們按下一個(gè)按鈕,將會(huì)執(zhí)行動(dòng)畫一次掠兄。

當(dāng)YES時(shí):當(dāng)上一次動(dòng)畫正在執(zhí)行中像云,那么當(dāng)下一個(gè)動(dòng)畫開始時(shí),上一次動(dòng)畫的當(dāng)前狀態(tài)將成為下一次動(dòng)畫的開始狀態(tài)蚂夕。
當(dāng)NO時(shí):當(dāng)上一個(gè)動(dòng)畫正在執(zhí)行中迅诬,那么當(dāng)下一個(gè)動(dòng)畫開始時(shí),上一次動(dòng)畫需要先恢復(fù)到完成時(shí)的狀態(tài)婿牍,然后在開始執(zhí)行下一次動(dòng)畫侈贷。
**setAnimationRepeatAutoreverses****方法**

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses

設(shè)置動(dòng)畫是否做一次反向的執(zhí)行。
如果設(shè)置為YES:動(dòng)畫將執(zhí)行:動(dòng)畫初始狀態(tài)》動(dòng)畫》動(dòng)畫完成狀態(tài)》動(dòng)畫》動(dòng)畫初始狀態(tài)等脂。
如果設(shè)置為NO:默認(rèn)值

setAnimationsEnabled**方法**

+ (void)setAnimationsEnabled:(BOOL)enabled

設(shè)置動(dòng)畫是否可用俏蛮!
YES:默認(rèn)值。
NO:動(dòng)畫效果被禁用
**注意:僅僅是動(dòng)畫是否可用上遥,在動(dòng)畫中被改變的UI對(duì)象依然是起作用的搏屑。僅僅是動(dòng)畫效果被禁用了。**

**areAnimationsEnabled****方法**

+ (BOOL)areAnimationsEnabled

// 刪除所有動(dòng)畫層
// 注意:層指的是layout露该,例:[_imageView.layer removeAllAnimations];
- (void)removeAllAnimations;
3睬棚、重要方法解析
/** * 設(shè)置動(dòng)畫過渡效果 * 
* @param transition 動(dòng)畫的過渡效果 
* @param view 過渡效果作用視圖 
* @param cache 如果為YES,開始和結(jié)束視圖分別渲染一次并在動(dòng)畫中創(chuàng)建幀解幼;否則抑党,視圖將會(huì)渲染每一幀。
例如撵摆,你不需要在視圖轉(zhuǎn)變中不停的更新底靠,你只需要等到轉(zhuǎn)換完成再去更新視圖。 */
+ (void)setAnimationTransition:(UIViewAnimationTransition)transition 
                       forView:(UIView *)view 
                         cache:(BOOL)cache;

二特铝、UIView的動(dòng)畫塊代碼

block 的方法主要有 7 個(gè)

這個(gè)是用的最多的 ( 主要在于參數(shù)的和回調(diào))

+ (void)animateWithDuration:(NSTimeInterval)duration 
                      delay:(NSTimeInterval)delay 
                    options:(UIViewAnimationOptions)options 
                  animations:(void (^)(void))animations 
                  completion:(void (^ __nullable)(BOOL finished))completion ;

[UIView animateWithDuration:(NSTimeInterval)          // 動(dòng)畫的持續(xù)時(shí)間
                      delay:(NSTimeInterval)          // 動(dòng)畫執(zhí)行的延遲時(shí)間
                    options:(UIViewAnimationOptions)  // 執(zhí)行的動(dòng)畫選項(xiàng)暑中,
                 animations:^{

        // 要執(zhí)行的動(dòng)畫代碼
 } completion:^(BOOL finished) {
        // 動(dòng)畫執(zhí)行完畢后的調(diào)用
}];

和上一個(gè)方法相比較,少了動(dòng)畫延遲和動(dòng)畫選項(xiàng) (使用的復(fù)雜度比上一個(gè)婿杲恕)

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0

[UIView animateWithDuration:(NSTimeInterval) 
                 animations:^{
        // 要執(zhí)行的動(dòng)畫代碼
 } completion:^(BOOL finished) {
        // 動(dòng)畫執(zhí)行完畢后的調(diào)用
 }];

這個(gè)是最簡(jiǎn)單的鳄逾,對(duì)動(dòng)畫的設(shè)置基本沒有,使用的都是默認(rèn)參數(shù)

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0, completion = NULL
    
    [UIView animateWithDuration:(NSTimeInterval) animations:^{
        
    }];

Spring Animationring Animation
在IOS7開始灵莲,系統(tǒng)動(dòng)畫效果廣泛應(yīng)用Spring Animation

+ (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 NS_AVAILABLE_IOS(7_0);
    
[UIView animateWithDuration:(NSTimeInterval)      // 動(dòng)畫持續(xù)時(shí)間
                      delay:(NSTimeInterval)    // 動(dòng)畫延遲時(shí)間
     usingSpringWithDamping:(CGFloat)    // 類似彈簧振動(dòng)效果 0~1
      initialSpringVelocity:(CGFloat)      // 初始速度
                    options:(UIViewAnimationOptions)   // 動(dòng)畫過渡效果
                 animations:^{
        // code
} completion:^(BOOL finished) {
        // code
}]

usingSpringWithDamping:它的范圍為 0.0f 到 1.0f 雕凹,數(shù)值越小「彈簧」的振動(dòng)效果越明顯。
initialSpringVelocity:初始的速度,數(shù)值越大一開始移動(dòng)越快枚抵。值得注意的是线欲,初始速度取值較高而時(shí)間較短時(shí),也會(huì)出現(xiàn)反彈情況汽摹。
Spring Animation 是線性動(dòng)畫或 ease-out 動(dòng)畫的理想替代品李丰。由于 iOS 本身大量使用的就是 Spring Animation,用戶已經(jīng)習(xí)慣了這種動(dòng)畫效果逼泣,因此使用它能使 App 讓人感覺更加自然趴泌,用 Apple 的話說就是「instantly familiar」。此外圾旨,Spring Animation 不只能對(duì)位置使用踱讨,它適用于所有可被添加動(dòng)畫效果的屬性魏蔗。
    + (void)transitionWithView:(UIView *)view
                       duration:(NSTimeInterval)duration
                        options:(UIViewAnimationOptions)options
                     animations:(void (^ __nullable)(void))animations
                     completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
    
    
    [UIView transitionWithView:(nonnull UIView *)
                      duration:(NSTimeInterval)
                       options:(UIViewAnimationOptions)options
                    animations:^{
        code
    } completion:^(BOOL finished) {
        code
    }];
 + (void)transitionFromView:(UIView *)fromView
                     toView:(UIView *)toView
                   duration:(NSTimeInterval)duration
                    options:(UIViewAnimationOptions)options
                 completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // toView added to fromView.superview, fromView removed from its superview
    
 [UIView transitionFromView:(nonnull UIView *)
                    toView:(nonnull UIView *)
                  duration:(NSTimeInterval)
                   options:(UIViewAnimationOptions)
                completion:^(BOOL finished) {
                        code
  }];
+ (void)performSystemAnimation:(UISystemAnimation)animation
                    onViews:(NSArray<__kindof UIView *> *)views
                    options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))parallelAnimations
                 completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);

[UIView performSystemAnimation:<(UISystemAnimation)>
                       onViews:(nonnull NSArray<__kindof UIView *> *)
                       options:<(UIViewAnimationOptions)>
                    animations:^{
    code
} completion:^(BOOL finished) {
    code
}]

三砍的、關(guān)鍵幀動(dòng)畫

關(guān)鍵幀動(dòng)畫
iOS 8 Spring Animation
UIView動(dòng)畫已經(jīng)具備高級(jí)的方法來創(chuàng)建動(dòng)畫,而且可以更好地理解和構(gòu)建動(dòng)畫莺治。
IOS7以后蘋果新加了一個(gè)animateKeyframesWithDuration的方法廓鞠,我們可以使用它來創(chuàng)建更多更復(fù)雜更酷炫的動(dòng)畫效果,而不需要去使用到核心動(dòng)畫(CoreAnimatino)谣旁。

/** * 添加關(guān)鍵幀方法 * 
* @param duration 動(dòng)畫時(shí)長(zhǎng) 
* @param delay 動(dòng)畫延遲 
* @param options 動(dòng)畫效果選項(xiàng) 
* @param animations 動(dòng)畫執(zhí)行代碼 
* @param completion 動(dòng)畫結(jié)束執(zhí)行代碼 */
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration 
                               delay:(NSTimeInterval)delay 
                             options:(UIViewKeyframeAnimationOptions)options 
                          animations:(void (^)(void))animations 
                          completion:(void (^)(BOOL finished))completion;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末床佳,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子榄审,更是在濱河造成了極大的恐慌砌们,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搁进,死亡現(xiàn)場(chǎng)離奇詭異浪感,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)饼问,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門影兽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人莱革,你說我怎么就攤上這事峻堰。” “怎么了盅视?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵捐名,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我闹击,道長(zhǎng)镶蹋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮梅忌,結(jié)果婚禮上狰腌,老公的妹妹穿的比我還像新娘。我一直安慰自己牧氮,他們只是感情好琼腔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著踱葛,像睡著了一般丹莲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上尸诽,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天甥材,我揣著相機(jī)與錄音,去河邊找鬼性含。 笑死洲赵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的商蕴。 我是一名探鬼主播叠萍,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼绪商!你這毒婦竟也來了苛谷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤格郁,失蹤者是張志新(化名)和其女友劉穎腹殿,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體例书,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锣尉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雾叭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悟耘。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖织狐,靈堂內(nèi)的尸體忽然破棺而出暂幼,到底是詐尸還是另有隱情,我是刑警寧澤移迫,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布旺嬉,位于F島的核電站,受9級(jí)特大地震影響厨埋,放射性物質(zhì)發(fā)生泄漏邪媳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望雨效。 院中可真熱鬧迅涮,春花似錦、人聲如沸徽龟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)据悔。三九已至传透,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間极颓,已是汗流浹背朱盐。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留菠隆,地道東北人兵琳。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像浸赫,于是被迫代替她去往敵國(guó)和親闰围。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

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