iOS實際開發(fā)中常用動畫:UIView動畫,核心動畫,幀動畫,自定義轉場動畫
1.UIView動畫
UIView動畫能夠設置的動畫屬性有:
frame
bounds
center
transform
alpha
BackgroundColor
contentStretch
UIView動畫實現(xiàn)方式有普通方式和Block
1.1普通方式
開始動畫語句
[UIView beginAnimations:(nullable NSString *) context:(nullable void *)];
// 第一個參數(shù): 動畫標識
// 第二個參數(shù): 附加參數(shù),在設置代理情況下宣渗,此參數(shù)將發(fā)送到setAnimationWillStartSelector和setAnimationDidStopSelector所指定的方法,大部分情況梨州,設置為nil.
結束動畫語句:
[UIView commitAnimations];
動畫持續(xù)時間
[UIView setAnimationDuration:(NSTimeInterval)];
動畫的代理對象
[UIView setAnimationDelegate:(nullable id)];
設置動畫將開始時代理對象執(zhí)行的SEL
[UIView setAnimationWillStartSelector:(nullable SEL)];
設置動畫延遲執(zhí)行的時間
[UIView setAnimationDelay:(NSTimeInterval)];
設置動畫的重復次數(shù)
[UIView setAnimationRepeatCount:(float)];
設置動畫的曲線
[UIView setAnimationCurve:(UIViewAnimationCurve)];
typedef enum {
UIViewAnimationCurveEaseInOut, // 慢進慢出(默認值)
UIViewAnimationCurveEaseIn, // 慢進
UIViewAnimationCurveEaseOut, // 慢出
UIViewAnimationCurveLinear // 勻速
} UIViewAnimationCurve;
設置是否從當前狀態(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ù)
typedef enum {
UIViewAnimationTransitionNone, //不使用動畫
UIViewAnimationTransitionFlipFromLeft, //從左向右旋轉翻頁
UIViewAnimationTransitionFlipFromRight, //從右向左旋轉翻頁
UIViewAnimationTransitionCurlUp, //從下往上卷曲翻頁
UIViewAnimationTransitionCurlDown, //從上往下卷曲翻頁
} UIViewAnimationTransition;
//第二個參數(shù),需要過渡效果的View
//第三個參數(shù),是否使用視圖緩存鞍恢,YES:視圖在開始和結束時渲染一次;NO:視圖在每一幀都渲染
代碼例子
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//觸摸點
UITouch *tuch = touches.anyObject;
CGPoint point = [tuch locationInView:self.view];
//開始動畫
[UIView beginAnimations:@"testAnimation" context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelegate:self];
//設置動畫將開始時代理對象執(zhí)行的SEL
[UIView setAnimationWillStartSelector:@selector(animationDoing)];
//設置動畫延遲執(zhí)行的時間
[UIView setAnimationDelay:0];
[UIView setAnimationRepeatCount:MAXFLOAT];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//設置動畫是否繼續(xù)執(zhí)行相反的動畫
[UIView setAnimationRepeatAutoreverses:YES];
self.redView.center = point;
self.redView.transform = CGAffineTransformMakeScale(1.5, 1.5);
self.redView.transform = CGAffineTransformMakeRotation(M_PI);
//結束動畫
[UIView commitAnimations];
}
1.2Block
ios4.0以后增加了Block動畫塊每窖,提供了更簡潔的方式來實現(xiàn)動畫.日常開發(fā)中一般也是使用Block形式創(chuàng)建動畫帮掉。最簡潔的Block動畫:包含時間和動畫:
[UIView animateWithDuration:(NSTimeInterval) //動畫持續(xù)時間
animations:^{
//執(zhí)行的動畫
}];
** 帶有動畫提交回調的Block動畫**
[UIView animateWithDuration:(NSTimeInterval) //動畫持續(xù)時間
animations:^{
//執(zhí)行的動畫
} completion:^(BOOL finished) {
//動畫執(zhí)行提交后的操作
}];
可以設置延時時間和過渡效果的Block動畫
[UIView animateWithDuration:(NSTimeInterval) //動畫持續(xù)時間
delay:(NSTimeInterval) //動畫延遲執(zhí)行的時間
options:(UIViewAnimationOptions) //動畫的過渡效果
animations:^{
//執(zhí)行的動畫
} completion:^(BOOL finished) {
//動畫執(zhí)行提交后的操作
}];
//可以組合使用
typedef enum {
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 //轉場充边,從下向上旋轉翻頁
} UIViewAnimationOptions;
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í)行提交后的操作
}];
Keyframes動畫:iOS7.0后新增了關鍵幀動畫聋亡,支持屬性關鍵幀湖饱,不支持路徑關鍵幀
[UIView animateKeyframesWithDuration:(NSTimeInterval)//動畫持續(xù)時間
delay:(NSTimeInterval)//動畫延遲執(zhí)行的時間
options:(UIViewKeyframeAnimationOptions)//動畫的過渡效果
animations:^{
//執(zhí)行的關鍵幀動畫
}
completion:^(BOOL finished) {
//動畫執(zhí)行提交后的操作
}];
//可組合使用:
typedef enum {
UIViewAnimationOptionLayoutSubviews //進行動畫時布局子控件
UIViewAnimationOptionAllowUserInteraction //進行動畫時允許用戶交互
UIViewAnimationOptionBeginFromCurrentState //從當前狀態(tài)開始動畫
UIViewAnimationOptionRepeat //無限重復執(zhí)行動畫
UIViewAnimationOptionAutoreverse //執(zhí)行動畫回路
UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套動畫的執(zhí)行時間設置
UIViewAnimationOptionOverrideInheritedOptions //不繼承父動畫設置
UIViewKeyframeAnimationOptionCalculationModeLinear //運算模式 :連續(xù)
UIViewKeyframeAnimationOptionCalculationModeDiscrete //運算模式 :離散
UIViewKeyframeAnimationOptionCalculationModePaced //運算模式 :均勻執(zhí)行
UIViewKeyframeAnimationOptionCalculationModeCubic //運算模式 :平滑
UIViewKeyframeAnimationOptionCalculationModeCubicPaced //運算模式 :平滑均勻
} UIViewKeyframeAnimationOptions;
增加關鍵幀方法:
[UIView addKeyframeWithRelativeStartTime:(double)//動畫開始的時間(占總時間的比例)
relativeDuration:(double) //動畫持續(xù)時間(占總時間的比例)
animations:^{
//執(zhí)行的動畫
}];
轉場動畫:從舊視圖到新視圖的動畫效果
[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];
單個視圖的過渡效果
[UIView transitionWithView:(nonnull UIView *)
duration:(NSTimeInterval)
options:(UIViewAnimationOptions)
animations:^{
//執(zhí)行的動畫
}
completion:^(BOOL finished) {
//動畫執(zhí)行提交后的操作
}];
代碼例子
[UIView animateWithDuration:3.0 animations:^{
self.redView.center = point;
self.redView.transform = CGAffineTransformMakeScale(1.5, 1.5);
self.redView.transform = CGAffineTransformMakeRotation(M_PI);
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 animations:^{
self.redView.frame = CGRectMake(100, 100, 100, 100);
self.redView.transform = CGAffineTransformMakeScale(1 / 1.5,1 / 1.5);
self.redView.transform = CGAffineTransformMakeRotation(M_PI);
}];
}];