iOS动画-从UIView到Core Animation
首先,介紹一下UIView相關的動畫。
動畫屬性設置:
1 //動畫持續時間 2 [UIView setAnimationDuration:(NSTimeInterval)]; 3 //動畫的代理對象 4 [UIView setAnimationDelegate:(nullable id)]; 5 //設置動畫將開始時代理對象執行的SEL 6 [UIView setAnimationWillStartSelector:(nullable SEL)]; 7 //設置動畫延遲執行的時間 8 [UIView setAnimationDelay:(NSTimeInterval)]; 9 //設置動畫的重復次數 10 [UIView setAnimationRepeatCount:(float)]; 11 //設置動畫的曲線 12 /* 13 UIViewAnimationCurve的枚舉值: 14 UIViewAnimationCurveEaseInOut, // 慢進慢出(默認值) 15 UIViewAnimationCurveEaseIn, // 慢進 16 UIViewAnimationCurveEaseOut, // 慢出 17 UIViewAnimationCurveLinear // 勻速 18 */ 19 [UIView setAnimationCurve:(UIViewAnimationCurve)]; 20 //設置是否從當前狀態開始播放動畫 21 /*假設上一個動畫正在播放,且尚未播放完畢,我們將要進行一個新的動畫: 22 當為YES時:動畫將從上一個動畫所在的狀態開始播放 23 當為NO時:動畫將從上一個動畫所指定的最終狀態開始播放(此時上一個動畫馬上結束)*/ 24 [UIView setAnimationBeginsFromCurrentState:YES]; 25 //設置動畫是否繼續執行相反的動畫 26 [UIView setAnimationRepeatAutoreverses:(BOOL)]; 27 //是否禁用動畫效果(對象屬性依然會被改變,只是沒有動畫效果) 28 [UIView setAnimationsEnabled:(BOOL)]; 29 //設置視圖的過渡效果 30 /* 第一個參數:UIViewAnimationTransition的枚舉值如下 31 UIViewAnimationTransitionNone, //不使用動畫 32 UIViewAnimationTransitionFlipFromLeft, //從左向右旋轉翻頁 33 UIViewAnimationTransitionFlipFromRight, //從右向左旋轉翻頁 34 UIViewAnimationTransitionCurlUp, //從下往上卷曲翻頁 35 UIViewAnimationTransitionCurlDown, //從上往下卷曲翻頁 36 第二個參數:需要過渡效果的View 37 第三個參數:是否使用視圖緩存,YES:視圖在開始和結束時渲染一次;NO:視圖在每一幀都渲染*/ 38 [UIView setAnimationTransition:(UIViewAnimationTransition) forView:(nonnull UIView *) cache:(BOOL)];舉2個例子:
1 [UIView beginAnimations:@"xxx" context:nil]; 2 [UIView setAnimationDuration:5]; 3 [UIView setAnimationRepeatCount:MAXFLOAT]; 4 [UIView setAnimationDelegate:self]; 5 [UIView setAnimationDelay:3]; 6 [UIView setAnimationWillStartSelector:@selector(animationWillStart)]; 7 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 8 [UIView setAnimationRepeatAutoreverses:YES]; 9 10 self.iView.frame = CGRectMake(200, 400, 100, 100); 11 12 [UIView commitAnimations]; 1 [UIView beginAnimations:@"xxx" context:nil]; 2 [UIView setAnimationDuration:2]; 3 [UIView setAnimationRepeatCount:MAXFLOAT]; 4 [UIView setAnimationDelegate:self]; 5 //[UIView setAnimationDelay:3]; 6 [UIView setAnimationWillStartSelector:@selector(animationWillStart)]; 7 [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 8 //[UIView setAnimationRepeatAutoreverses:YES]; 9 10 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.iView cache:YES]; 11 12 [UIView commitAnimations];?
? ? ? 2. ? UIView Block動畫
1 [UIView animateWithDuration:(NSTimeInterval)//動畫時間 2 animations:^{ 3 //執行的動畫 4 }]; 1 [UIView animateWithDuration:(NSTimeInterval)//動畫時間 2 animations:^{ 3 //執行的動畫 4 } completion:^(BOOL finished) { 5 //動畫結束的回調 6 }]; 1 [UIView animateWithDuration:(NSTimeInterval)//動畫時間 2 delay:(NSTimeInterval)//動畫延遲時間 3 options:(UIViewAnimationOptions)//動畫過渡效果 4 animations:^{ 5 //執行動畫 6 } completion:^(BOOL finished) { 7 //動畫結束回調 8 }] UIViewAnimationOptions的枚舉值: UIViewAnimationOptionLayoutSubviews //進行動畫時布局子控件UIViewAnimationOptionAllowUserInteraction //進行動畫時允許用戶交互UIViewAnimationOptionBeginFromCurrentState //從當前狀態開始動畫UIViewAnimationOptionRepeat //無限重復執行動畫UIViewAnimationOptionAutoreverse //執行動畫回路UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套動畫的執行時間設置UIViewAnimationOptionOverrideInheritedCurve //忽略嵌套動畫的曲線設置UIViewAnimationOptionAllowAnimatedContent //轉場:進行動畫時重繪視圖UIViewAnimationOptionShowHideTransitionViews //轉場:移除(添加和移除圖層的)動畫效果UIViewAnimationOptionOverrideInheritedOptions //不繼承父動畫設置 UIViewAnimationOptionCurveEaseInOut //時間曲線,慢進慢出(默認值)UIViewAnimationOptionCurveEaseIn //時間曲線,慢進UIViewAnimationOptionCurveEaseOut //時間曲線,慢出UIViewAnimationOptionCurveLinear //時間曲線,勻速 UIViewAnimationOptionTransitionNone //轉場,不使用動畫UIViewAnimationOptionTransitionFlipFromLeft //轉場,從左向右旋轉翻頁UIViewAnimationOptionTransitionFlipFromRight //轉場,從右向左旋轉翻頁UIViewAnimationOptionTransitionCurlUp //轉場,下往上卷曲翻頁UIViewAnimationOptionTransitionCurlDown //轉場,從上往下卷曲翻頁UIViewAnimationOptionTransitionCrossDissolve //轉場,交叉消失和出現UIViewAnimationOptionTransitionFlipFromTop //轉場,從上向下旋轉翻頁UIViewAnimationOptionTransitionFlipFromBottom //轉場,從下向上旋轉翻頁這3個動畫比較簡單,不再多做敘述。
?
?
Spring動畫
ios7.0以后新增了Spring動畫(IOS系統動畫大部分采用Spring Animation, 適用所有可被添加動畫效果的屬性)
?
Keyframes動畫:
IOS7.0后新增了關鍵幀動畫,支持屬性關鍵幀,不支持路徑關鍵幀[UIView animateKeyframesWithDuration:(NSTimeInterval)//動畫持續時間delay:(NSTimeInterval)//動畫延遲執行的時間options:(UIViewKeyframeAnimationOptions)//動畫的過渡效果animations:^{//執行的關鍵幀動畫 }completion:^(BOOL finished) {//動畫執行提交后的操作}];?
Core Animation
CABasicAnimation:
// 常用屬性: duration:動畫的持續時間repeatCount:重復次數,無限循環可以設置HUGE_VALF或者MAXFLOATrepeatDuration:重復時間speed:動畫速率,決定動畫時間的倍率。當speed為2時,動畫時間為設置的duration的1/2。timeOffset:動畫時間偏移量。比如設置動畫時長為3秒,當設置timeOffset為1.5時,當前動畫會從中間位置開始,并在到達指定位置時,走完之前跳過的前半段動畫。removedOnCompletion:默認為YES,代表動畫執行完畢后就從圖層上移除,圖形會恢復到動畫執行前的狀態。如果想讓圖層保持顯示動畫執行后的狀態,那就設置為NO,不過還要設置fillMode為kCAFillModeForwardsfillMode:決定當前對象在非active時間段的行為。比如動畫開始之前或者動畫結束之后kCAFillModeRemoved:這個是默認值,也就是說當動畫開始前和動畫結束后,動畫對layer都沒有影響,動畫結束后,layer會恢復到之前的狀態kCAFillModeForwards:當動畫結束后,layer會一直保持著動畫最后的狀態kCAFillModeBackwards:在動畫開始前,只需要將動畫加入了一個layer,layer便立即進入動畫的初始狀態并等待動畫開始。kCAFillModeBoth:這個其實就是上面兩個的合成.動畫加入后開始之前,layer便處于動畫初始狀態,動畫結束后layer保持動畫最后的狀態beginTime:可以用來設置動畫延遲執行時間,若想延遲2s,就設置為CACurrentMediaTime()+2,CACurrentMediaTime()為圖層的當前時間timingFunction:速度控制函數,控制動畫運行的節奏kCAMediaTimingFunctionLinear(線性):勻速,給你一個相對靜態的感覺kCAMediaTimingFunctionEaseIn(漸進):動畫緩慢進入,然后加速離開kCAMediaTimingFunctionEaseOut(漸出):動畫全速進入,然后減速的到達目的地kCAMediaTimingFunctionEaseInEaseOut(漸進漸出):動畫緩慢的進入,中間加速,然后減速的到達目的地。這個是默認的動畫行為。delegate:動畫代理autoreverses:動畫完成后是否以動畫形式回到初始值fromValue:keyPath相應屬性的初始值toValue:keyPath相應屬性的結束值byValue:keyPath相應屬性的改變值`fromValue`,`toValue`和`byValue`屬性可以用很多種方式來組合,但為了防止沖突,不能一次性同時指定這三個值。例如,如果指定了`fromValue`等于2,`toValue`等于4,`byValue`等于3,那么Core Animation就不知道結果到底是4(`toValue`)還是5(`fromValue + byValue`)了。他們的用法在`CABasicAnimation`頭文件中已經描述的很清楚了,所以在這里就不重復了。總的說來,就是只需要指定`toValue`或者`byValue`,剩下的值都可以通過上下文自動計算出來。實例化方法:
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
其中keyPath表示動畫類型,常用的keyPath:
?
舉一個例子:
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; basicAnimation.duration = 5; basicAnimation.removedOnCompletion = NO; basicAnimation.fillMode = kCAFillModeForwards;// basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)];[self.iView.layer addAnimation:basicAnimation forKey:@"rotation"];“position”也可以替換成上圖其他的值,"fromvalue"/"toValue"/"byValue"要與動畫類型'keypath'對應。
?
CAKeyframeAnimation:關鍵幀動畫
關鍵幀動畫和CABasicAnimation一樣是CApropertyAnimation的子類,但是CABasicAnimation只能從一個數值(fromValue)變到另一個數值(toValue)或者添加一個增量數值(byValue),而CAKeyframeAnimation使用values數組可以設置多個關鍵幀,同時可以利用path可以進行位置或者錨點的動畫操作。
常用屬性:
values:關鍵幀數組對象,里面每一個元素即為一個關鍵幀,動畫會在對應的時間段內,依次執行數組中每一個關鍵幀的動畫。 path:動畫路徑對象,可以指定一個路徑,在執行動畫時路徑會沿著路徑移動,Path在動畫中只會影響視圖的Position。 keyTimes:設置關鍵幀對應的時間點,范圍:0-1。如果沒有設置該屬性,則每一幀的時間平分。timingFunctions:每一幀對應的動畫節奏。
rotationMode:動畫沿路徑旋轉方式。例如讓一個視圖按照一條三次貝塞爾曲線移動,如果rotationMode=kCAAnimationRotateAuto,那么這個視圖會沿著曲線的切線移動,而不是直來直去的。如下圖所示:
?
?
舉例:
if (tag == 6) {//使用“values”keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];keyFrameAni.duration = 0.3;keyFrameAni.values = @[@(-(4) / 180.0*M_PI),@((4) / 180.0*M_PI),@(-(4) / 180.0*M_PI)];keyFrameAni.repeatCount=MAXFLOAT;}else if (tag == 7){//使用“path”路徑keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:_aniLayer.position];[path addCurveToPoint:CGPointMake(300, 500) controlPoint1:CGPointMake(100, 400) controlPoint2:CGPointMake(300, 450)];keyFrameAni.path = path.CGPath;keyFrameAni.duration = 1; 1}[_aniLayer addAnimation:keyFrameAni forKey:@"keyFrameAnimation"];?
CASpringAnimation
CASpringAnimation是iOS9才引入的動畫類,效果類似于UIView的spring動畫,不過比其增加了質量,勁度系數等屬性的擴展,繼承于CABaseAnimation,用法也很簡單:
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position"];//質量,影響圖層運動時的彈簧慣性,質量越大,彈簧拉伸和壓縮的幅度越大springAnimation.mass = 1;//剛度系數(勁度系數/彈性系數),剛度系數越大,形變產生的力就越大,運動越快springAnimation.stiffness = 10;//阻尼系數,阻止彈簧伸縮的系數,阻尼系數越大,停止越快springAnimation.damping = 3;//初始速率,動畫視圖的初始速度大小 Defaults to zero//速率為正數時,速度方向與運動方向一致,速率為負數時,速度方向與運動方向相反springAnimation.initialVelocity = 10;//settlingDuration:估算時間 返回彈簧動畫到停止時的估算時間,根據當前的動畫參數估算springAnimation.duration = springAnimation.settlingDuration;springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(100, 400, 100, 100)];[self.iView.layer addAnimation:springAnimation forKey:@"spring"];?
CATransition:轉場動畫
CATransition是CAAnimation的子類,用于做轉場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果。iOS比Mac OS X的轉場動畫效果少一點UINavigationController就是通過CATransition實現了將控制器的視圖推入屏幕的動畫效果
動畫屬性:(有的屬性是具備方向的,詳情看下圖)
type:動畫過渡類型
subtype:動畫過渡方向
startProgress:動畫起點(在整體動畫的百分比)
endProgress:動畫終點(在整體動畫的百分比) -(void)transitionAnimation{self.index++;if (self.index>7) {self.index=1;}self.iImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[NSNumber numberWithInteger:self.index]]];CATransition *transition = [CATransition animation];transition.type = @"cube";//transition.subtype = @"fromRight";transition.duration = 2;[self.iImageView.layer addAnimation:transition forKey:nil]; }
這里是以"cube"和"pageCurl"的效果圖,就是上面的代碼,沒有做成gif圖,可以想象一下哈!
?
CAAnimationGroup:動畫組
動畫組,是CAAnimation的子類,可以保存一組動畫對象,將CAAnimationGroup對象加入層后,組中所有動畫對象可以同時并發運行
屬性說明:
animations:用來保存一組動畫對象的NSArray
默認情況下,一組動畫對象是同時運行的,也可以通過設置動畫對象的beginTime屬性來更改動畫的開始時間
示例:
CAAnimationGroup *group = [CAAnimationGroup animation];// 創建旋轉動畫對象CABasicAnimation *retate = [CABasicAnimation animation];// layer的旋轉屬性retate.keyPath = @"transform.rotation";// 角度retate.toValue = @(M_PI);// 創建縮放動畫對象CABasicAnimation *scale = [CABasicAnimation animation];// 縮放屬性scale.keyPath = @"transform.scale";// 縮放比例scale.toValue = @(0.0);// 添加到動畫組當中group.animations = @[retate,scale];// 執行動畫時間group.duration = 2.0;// 執行完以后不要刪除動畫group.removedOnCompletion = NO;// 保持最新的狀態group.fillMode = kCAFillModeForwards;[self.view.layer addAnimation:group forKey:nil];?
?
文章轉載:https://www.jianshu.com/p/9fa025c42261
轉載于:https://www.cnblogs.com/lfyDragon/p/8796518.html
總結
以上是生活随笔為你收集整理的iOS动画-从UIView到Core Animation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】 差分约束系统详解(转化为最短路)
- 下一篇: 爬虫(十二):scrapy中spider