注意點(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;