UIView動(dòng)畫是對(duì)CoreAnimation的封裝,提供簡(jiǎn)介的動(dòng)畫借口。
UIView動(dòng)畫可以設(shè)置的屬性:
- frame 大小變化
- bounds 拉伸變化
- center 中心位置
- transform 旋轉(zhuǎn)
- alpha 透明度
- backgroundColor 背景顏色
- contentStretch 拉伸內(nèi)容
1、 UIView的Block動(dòng)畫
1.1、簡(jiǎn)單的Block動(dòng)畫
[UIView animateWithDuration:(NSTimeInterval)//動(dòng)畫持續(xù)時(shí)間
animations:^{
//執(zhí)行的動(dòng)畫
}];
[UIView animateWithDuration:(NSTimeInterval)//動(dòng)畫持續(xù)時(shí)間
animations:^{
//執(zhí)行的動(dòng)畫
}completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
1.2 、可設(shè)置延遲時(shí)間和過渡效果的Block動(dòng)畫
[UIView animateWithDuration:(NSTimeInterval)//動(dòng)畫持續(xù)時(shí)間
delay:(NSTimeInterval)//動(dòng)畫延遲執(zhí)行的時(shí)間
options:(UIViewAnimationOptions)//動(dòng)畫的過渡效果
animations:^{
//執(zhí)行的動(dòng)畫
}completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
UIViewAnimationOptions的枚舉值如下,可組合使用:
UIViewAnimationOptionLayoutSubviews//進(jìn)行動(dòng)畫時(shí)布局子控件
UIViewAnimationOptionAllowUserInteraction//進(jìn)行動(dòng)畫時(shí)允許用戶交互
UIViewAnimationOptionBeginFromCurrentState//從當(dāng)前狀態(tài)開始動(dòng)畫
UIViewAnimationOptionRepeat//無限重復(fù)執(zhí)行動(dòng)畫
UIViewAnimationOptionAutoreverse//執(zhí)行動(dòng)畫回路
UIViewAnimationOptionOverrideInheritedDuration//忽略嵌套動(dòng)畫的執(zhí)行時(shí)間設(shè)置
UIViewAnimationOptionOverrideInheritedCurve//忽略嵌套動(dòng)畫的曲線設(shè)置
UIViewAnimationOptionAllowAnimatedContent//轉(zhuǎn)場(chǎng):進(jìn)行動(dòng)畫時(shí)重繪視圖
UIViewAnimationOptionShowHideTransitionViews//轉(zhuǎn)場(chǎng):移除(添加和移除圖層的)動(dòng)畫效果
UIViewAnimationOptionOverrideInheritedOptions//不繼承父動(dòng)畫設(shè)置
UIViewAnimationOptionCurveEaseInOut//時(shí)間曲線,慢進(jìn)慢出(默認(rèn)值)
UIViewAnimationOptionCurveEaseIn//時(shí)間曲線,慢進(jìn)
UIViewAnimationOptionCurveEaseOut//時(shí)間曲線,慢出
UIViewAnimationOptionCurveLinear//時(shí)間曲線,勻速
UIViewAnimationOptionTransitionNone//轉(zhuǎn)場(chǎng),不使用動(dòng)畫
UIViewAnimationOptionTransitionFlipFromLeft//轉(zhuǎn)場(chǎng),從左向右旋轉(zhuǎn)翻頁(yè)
UIViewAnimationOptionTransitionFlipFromRight//轉(zhuǎn)場(chǎng),從右向左旋轉(zhuǎn)翻頁(yè)
UIViewAnimationOptionTransitionCurlUp//轉(zhuǎn)場(chǎng),下往上卷曲翻頁(yè)
UIViewAnimationOptionTransitionCurlDown//轉(zhuǎn)場(chǎng),從上往下卷曲翻頁(yè)
UIViewAnimationOptionTransitionCrossDissolve//轉(zhuǎn)場(chǎng),交叉消失和出現(xiàn)
UIViewAnimationOptionTransitionFlipFromTop//轉(zhuǎn)場(chǎng),從上向下旋轉(zhuǎn)翻頁(yè)
UIViewAnimationOptionTransitionFlipFromBottom//轉(zhuǎn)場(chǎng),從下向上旋轉(zhuǎn)翻頁(yè)
1.3、Spring動(dòng)畫
[UIView animateWithDuration:(NSTimeInterval)//動(dòng)畫持續(xù)時(shí)間
delay:(NSTimeInterval)//動(dòng)畫延遲執(zhí)行的時(shí)間
usingSpringWithDamping:(CGFloat)//震動(dòng)效果,范圍0~1,數(shù)值越小震動(dòng)效果越明顯
initialSpringVelocity:(CGFloat)//初始速度,數(shù)值越大初始速度越快
options:(UIViewAnimationOptions)//動(dòng)畫的過渡效果
animations:^{
//執(zhí)行的動(dòng)畫
}
completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
1.4、Keyframes動(dòng)畫
[UIView animateKeyframesWithDuration:(NSTimeInterval)//動(dòng)畫持續(xù)時(shí)間
delay:(NSTimeInterval)//動(dòng)畫延遲執(zhí)行的時(shí)間
options:(UIViewKeyframeAnimationOptions)//動(dòng)畫的過渡效果
animations:^{
//執(zhí)行的關(guān)鍵幀動(dòng)畫
}
completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
UIViewKeyframeAnimationOptions的枚舉值如下,可組合使用:
UIViewAnimationOptionLayoutSubviews//進(jìn)行動(dòng)畫時(shí)布局子控件
UIViewAnimationOptionAllowUserInteraction//進(jìn)行動(dòng)畫時(shí)允許用戶交互
UIViewAnimationOptionBeginFromCurrentState//從當(dāng)前狀態(tài)開始動(dòng)畫
UIViewAnimationOptionRepeat//無限重復(fù)執(zhí)行動(dòng)畫
UIViewAnimationOptionAutoreverse//執(zhí)行動(dòng)畫回路
UIViewAnimationOptionOverrideInheritedDuration//忽略嵌套動(dòng)畫的執(zhí)行時(shí)間設(shè)置
UIViewAnimationOptionOverrideInheritedOptions//不繼承父動(dòng)畫設(shè)置
UIViewKeyframeAnimationOptionCalculationModeLinear//運(yùn)算模式:連續(xù)
UIViewKeyframeAnimationOptionCalculationModeDiscrete//運(yùn)算模式:離散
UIViewKeyframeAnimationOptionCalculationModePaced//運(yùn)算模式:均勻執(zhí)行
UIViewKeyframeAnimationOptionCalculationModeCubic//運(yùn)算模式:平滑
UIViewKeyframeAnimationOptionCalculationModeCubicPaced//運(yùn)算模式:平滑均勻
1.6 轉(zhuǎn)場(chǎng)動(dòng)畫
1.6.1 從舊視圖轉(zhuǎn)到新視圖的動(dòng)畫效果
//在該動(dòng)畫過程中,fromView會(huì)從父視圖中移除,并講toView添加到父視圖中,注意轉(zhuǎn)場(chǎng)動(dòng)畫的作用對(duì)象是父視圖(過渡效果體現(xiàn)在父視圖上)。
[UIView transitionFromView:(nonnullUIView*)
toView:(nonnullUIView*)
duration:(NSTimeInterval)
options:(UIViewAnimationOptions)
completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
1.6.2單個(gè)視圖的過渡效果
[UIViewtransitionWithView:(nonnullUIView*)
duration:(NSTimeInterval)
options:(UIViewAnimationOptions)
animations:^{
//執(zhí)行的動(dòng)畫
}
completion:^(BOOLfinished){
//動(dòng)畫執(zhí)行完畢后的操作
}];
2. UIView的類方法動(dòng)畫
[UIView beginAnimations:(nullableNSString*)context:(nullablevoid*)]; //開始動(dòng)畫
第一個(gè)參數(shù):動(dòng)畫標(biāo)識(shí)
第二個(gè)參數(shù):附加參數(shù),在設(shè)置了代理的情況下,此參數(shù)將發(fā)送到setAnimationWillStartSelector和setAnimationDidStopSelector所指定的方法。大部分情況下,我們?cè)O(shè)置為nil即可。
[UIView commitAnimations]; //動(dòng)畫結(jié)束
動(dòng)畫參數(shù)的設(shè)置方法
//動(dòng)畫持續(xù)時(shí)間
[UIViewsetAnimationDuration:(NSTimeInterval)];
//動(dòng)畫的代理對(duì)象
[UIViewsetAnimationDelegate:(nullableid)];
//設(shè)置動(dòng)畫將開始時(shí)代理對(duì)象執(zhí)行的SEL
[UIViewsetAnimationWillStartSelector:(nullableSEL)];
//設(shè)置動(dòng)畫結(jié)束時(shí)代理對(duì)象執(zhí)行的SEL
[UIViewsetAnimationDidStopSelector:(nullableSEL)];
//設(shè)置動(dòng)畫延遲執(zhí)行的時(shí)間
[UIViewsetAnimationDelay:(NSTimeInterval)];
//設(shè)置動(dòng)畫的重復(fù)次數(shù)
[UIViewsetAnimationRepeatCount:(float)];
//設(shè)置動(dòng)畫的曲線
[UIViewsetAnimationCurve:(UIViewAnimationCurve)];
UIViewAnimationCurve的枚舉值如下:
UIViewAnimationCurveEaseInOut,//慢進(jìn)慢出(默認(rèn)值)
UIViewAnimationCurveEaseIn,//慢進(jìn)
UIViewAnimationCurveEaseOut,//慢出
UIViewAnimationCurveLinear//勻速
//設(shè)置是否從當(dāng)前狀態(tài)開始播放動(dòng)畫
[UIViewsetAnimationBeginsFromCurrentState:YES];
//當(dāng)為YES時(shí):動(dòng)畫將從上一個(gè)動(dòng)畫所在的狀態(tài)開始播放
//當(dāng)為NO時(shí):動(dòng)畫將從上一個(gè)動(dòng)畫所指定的最終狀態(tài)開始播放(此時(shí)上一個(gè)動(dòng)畫馬上結(jié)束)
//設(shè)置動(dòng)畫是否繼續(xù)執(zhí)行相反的動(dòng)畫
[UIViewsetAnimationRepeatAutoreverses:(BOOL)];
//是否禁用動(dòng)畫效果(對(duì)象屬性依然會(huì)被改變,只是沒有動(dòng)畫效果)
[UIViewsetAnimationsEnabled:(BOOL)];
//設(shè)置視圖的過渡效果
[UIViewsetAnimationTransition:(UIViewAnimationTransition)forView:(nonnullUIView*)cache:(BOOL)];
第一個(gè)參數(shù):UIViewAnimationTransition的枚舉值如下
UIViewAnimationTransitionNone,//不使用動(dòng)畫
UIViewAnimationTransitionFlipFromLeft,//從左向右旋轉(zhuǎn)翻頁(yè)
UIViewAnimationTransitionFlipFromRight,//從右向左旋轉(zhuǎn)翻頁(yè)
UIViewAnimationTransitionCurlUp,//從下往上卷曲翻頁(yè)
UIViewAnimationTransitionCurlDown,//從上往下卷曲翻頁(yè)
第二個(gè)參數(shù):需要過渡效果的View
第三個(gè)參數(shù):是否使用視圖緩存,YES:視圖在開始和結(jié)束時(shí)渲染一次;NO:視圖在每一幀都渲染
屬性變化動(dòng)畫(以frame變化為例)實(shí)例
[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];
轉(zhuǎn)場(chǎng)效果動(dòng)畫(以Flip效果為例)
[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:UIViewAnimationTransitionFlipFromLeftforView:self.centerShowcache:YES];
self.centerShow.image=[UIImageimageNamed:@"service"];
[UIView commitAnimations];