iOS常用动画
轉自:http://www.open-open.com/lib/view/open1372152939072.html
讀書人學習不算偷
1 #import <Foundation/Foundation.h> 2 3 /** 4 ! 導入QuartzCore.framework 5 * 6 * Example: 7 * 8 * Step.1 9 * 10 * #import "CoreAnimationEffect.h" 11 * 12 * Step.2 13 * 14 * [CoreAnimationEffect animationMoveLeft:your view]; 15 * 16 */ 17 18 19 @interface CoreAnimationEffect : NSObject 20 21 #pragma mark - Custom Animation 22 23 /** 24 * @brief 快速構建一個你自定義的動畫,有以下參數供你設置. 25 * 26 * @note 調用系統預置Type需要在調用類引入下句 27 * 28 * #import <QuartzCore/QuartzCore.h> 29 * 30 * @param type 動畫過渡類型 31 * @param subType 動畫過渡方向(子類型) 32 * @param duration 動畫持續時間 33 * @param timingFunction 動畫定時函數屬性 34 * @param theView 需要添加動畫的view. 35 * 36 * 37 */ 38 39 + (void)showAnimationType:(NSString *)type 40 withSubType:(NSString *)subType 41 duration:(CFTimeInterval)duration 42 timingFunction:(NSString *)timingFunction 43 view:(UIView *)theView; 44 45 #pragma mark - Preset Animation 46 47 /** 48 * 下面是一些常用的動畫效果 49 */ 50 51 // reveal 52 + (void)animationRevealFromBottom:(UIView *)view; 53 + (void)animationRevealFromTop:(UIView *)view; 54 + (void)animationRevealFromLeft:(UIView *)view; 55 + (void)animationRevealFromRight:(UIView *)view; 56 57 // 漸隱漸消 58 + (void)animationEaseIn:(UIView *)view; 59 + (void)animationEaseOut:(UIView *)view; 60 61 // 翻轉 62 + (void)animationFlipFromLeft:(UIView *)view; 63 + (void)animationFlipFromRigh:(UIView *)view; 64 65 // 翻頁 66 + (void)animationCurlUp:(UIView *)view; 67 + (void)animationCurlDown:(UIView *)view; 68 69 // push 70 + (void)animationPushUp:(UIView *)view; 71 + (void)animationPushDown:(UIView *)view; 72 + (void)animationPushLeft:(UIView *)view; 73 + (void)animationPushRight:(UIView *)view; 74 75 // move 76 + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration; 77 + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration; 78 + (void)animationMoveLeft:(UIView *)view; 79 + (void)animationMoveRight:(UIView *)view; 80 81 // 旋轉縮放 82 83 // 各種旋轉縮放效果 84 + (void)animationRotateAndScaleEffects:(UIView *)view; 85 86 // 旋轉同時縮小放大效果 87 + (void)animationRotateAndScaleDownUp:(UIView *)view; 88 89 90 91 #pragma mark - Private API 92 93 /** 94 * 下面動畫里用到的某些屬性在當前API里是不合法的,但是也可以用. 95 */ 96 97 + (void)animationFlipFromTop:(UIView *)view; 98 + (void)animationFlipFromBottom:(UIView *)view; 99 100 + (void)animationCubeFromLeft:(UIView *)view; 101 + (void)animationCubeFromRight:(UIView *)view; 102 + (void)animationCubeFromTop:(UIView *)view; 103 + (void)animationCubeFromBottom:(UIView *)view; 104 105 + (void)animationSuckEffect:(UIView *)view; 106 107 + (void)animationRippleEffect:(UIView *)view; 108 109 + (void)animationCameraOpen:(UIView *)view; 110 + (void)animationCameraClose:(UIView *)view; 111 112 @end 113 114 115 116 // 117 // CoreAnimationEffect.m 118 // CoreAnimationEffect 119 // 120 // Created by VincentXue on 13-1-19. 121 // Copyright (c) 2013年 VincentXue. All rights reserved. 122 // 123 124 #import "CoreAnimationEffect.h" 125 126 #import <QuartzCore/QuartzCore.h> 127 128 @implementation CoreAnimationEffect 129 130 /** 131 * 首先推薦一個不錯的網站. http://www.raywenderlich.com 132 */ 133 134 #pragma mark - Custom Animation 135 136 + (void)showAnimationType:(NSString *)type 137 withSubType:(NSString *)subType 138 duration:(CFTimeInterval)duration 139 timingFunction:(NSString *)timingFunction 140 view:(UIView *)theView 141 { 142 /** CATransition 143 * 144 * @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html 145 * @see http://geeklu.com/2012/09/animation-in-ios/ 146 * 147 * CATransition 常用設置及屬性注解如下: 148 */ 149 150 CATransition *animation = [CATransition animation]; 151 152 /** delegate 153 * 154 * 動畫的代理,如果你想在動畫開始和結束的時候做一些事,可以設置此屬性,它會自動回調兩個代理方法. 155 * 156 * @see CAAnimationDelegate (按下command鍵點擊) 157 */ 158 159 animation.delegate = self; 160 161 /** duration 162 * 163 * 動畫持續時間 164 */ 165 166 animation.duration = duration; 167 168 /** timingFunction 169 * 170 * 用于變化起點和終點之間的插值計算,形象點說它決定了動畫運行的節奏,比如是均勻變化(相同時間變化量相同)還是 171 * 先快后慢,先慢后快還是先慢再快再慢. 172 * 173 * 動畫的開始與結束的快慢,有五個預置分別為(下同): 174 * kCAMediaTimingFunctionLinear 線性,即勻速 175 * kCAMediaTimingFunctionEaseIn 先慢后快 176 * kCAMediaTimingFunctionEaseOut 先快后慢 177 * kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢 178 * kCAMediaTimingFunctionDefault 實際效果是動畫中間比較快. 179 */ 180 181 /** timingFunction 182 * 183 * 當上面的預置不能滿足你的需求的時候,你可以使用下面的兩個方法來自定義你的timingFunction 184 * 具體參見下面的URL 185 * 186 * @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html 187 * 188 * + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 189 * 190 * - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 191 */ 192 193 animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction]; 194 195 /** fillMode 196 * 197 * 決定當前對象過了非active時間段的行為,比如動畫開始之前,動畫結束之后. 198 * 預置為: 199 * kCAFillModeRemoved 默認,當動畫開始前和動畫結束后,動畫對layer都沒有影響,動畫結束后,layer會恢復到之前的狀態 200 * kCAFillModeForwards 當動畫結束后,layer會一直保持著動畫最后的狀態 201 * kCAFillModeBackwards 和kCAFillModeForwards相對,具體參考上面的URL 202 * kCAFillModeBoth kCAFillModeForwards和kCAFillModeBackwards在一起的效果 203 */ 204 205 animation.fillMode = kCAFillModeForwards; 206 207 /** removedOnCompletion 208 * 209 * 這個屬性默認為YES.一般情況下,不需要設置這個屬性. 210 * 211 * 但如果是CAAnimation動畫,并且需要設置 fillMode 屬性,那么需要將 removedOnCompletion 設置為NO,否則 212 * fillMode無效 213 */ 214 215 // animation.removedOnCompletion = NO; 216 217 /** type 218 * 219 * 各種動畫效果 其中除了'fade', `moveIn', `push' , `reveal' ,其他屬于似有的API(我是這么認為的,可以點進去看下注釋). 220 * ↑↑↑上面四個可以分別使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'來調用. 221 * @"cube" 立方體翻滾效果 222 * @"moveIn" 新視圖移到舊視圖上面 223 * @"reveal" 顯露效果(將舊視圖移開,顯示下面的新視圖) 224 * @"fade" 交叉淡化過渡(不支持過渡方向) (默認為此效果) 225 * @"pageCurl" 向上翻一頁 226 * @"pageUnCurl" 向下翻一頁 227 * @"suckEffect" 收縮效果,類似系統最小化窗口時的神奇效果(不支持過渡方向) 228 * @"rippleEffect" 滴水效果,(不支持過渡方向) 229 * @"oglFlip" 上下左右翻轉效果 230 * @"rotate" 旋轉效果 231 * @"push" 232 * @"cameraIrisHollowOpen" 相機鏡頭打開效果(不支持過渡方向) 233 * @"cameraIrisHollowClose" 相機鏡頭關上效果(不支持過渡方向) 234 */ 235 236 /** type 237 * 238 * kCATransitionFade 交叉淡化過渡 239 * kCATransitionMoveIn 新視圖移到舊視圖上面 240 * kCATransitionPush 新視圖把舊視圖推出去 241 * kCATransitionReveal 將舊視圖移開,顯示下面的新視圖 242 */ 243 244 animation.type = type; 245 246 /** subtype 247 * 248 * 各種動畫方向 249 * 250 * kCATransitionFromRight; 同字面意思(下同) 251 * kCATransitionFromLeft; 252 * kCATransitionFromTop; 253 * kCATransitionFromBottom; 254 */ 255 256 /** subtype 257 * 258 * 當type為@"rotate"(旋轉)的時候,它也有幾個對應的subtype,分別為: 259 * 90cw 逆時針旋轉90° 260 * 90ccw 順時針旋轉90° 261 * 180cw 逆時針旋轉180° 262 * 180ccw 順時針旋轉180° 263 */ 264 265 /** 266 * type與subtype的對應關系(必看),如果對應錯誤,動畫不會顯現. 267 * 268 * @see http://iphonedevwiki.net/index.php/CATransition 269 */ 270 271 animation.subtype = subType; 272 273 /** 274 * 所有核心動畫和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把動畫添加到layer上. 275 * forKey 可以是任意字符串. 276 */ 277 278 [theView.layer addAnimation:animation forKey:nil]; 279 } 280 281 #pragma mark - Preset Animation 282 283 284 + (void)animationRevealFromBottom:(UIView *)view 285 { 286 CATransition *animation = [CATransition animation]; 287 [animation setDuration:0.35f]; 288 [animation setType:kCATransitionReveal]; 289 [animation setSubtype:kCATransitionFromBottom]; 290 [animation setFillMode:kCAFillModeForwards]; 291 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 292 293 [view.layer addAnimation:animation forKey:nil]; 294 } 295 296 + (void)animationRevealFromTop:(UIView *)view 297 { 298 CATransition *animation = [CATransition animation]; 299 [animation setDuration:0.35f]; 300 [animation setType:kCATransitionReveal]; 301 [animation setSubtype:kCATransitionFromTop]; 302 [animation setFillMode:kCAFillModeForwards]; 303 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 304 305 [view.layer addAnimation:animation forKey:nil]; 306 } 307 308 + (void)animationRevealFromLeft:(UIView *)view 309 { 310 CATransition *animation = [CATransition animation]; 311 [animation setDuration:0.35f]; 312 [animation setType:kCATransitionReveal]; 313 [animation setSubtype:kCATransitionFromLeft]; 314 [animation setFillMode:kCAFillModeForwards]; 315 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 316 317 [view.layer addAnimation:animation forKey:nil]; 318 } 319 320 + (void)animationRevealFromRight:(UIView *)view 321 { 322 CATransition *animation = [CATransition animation]; 323 [animation setDuration:0.35f]; 324 [animation setType:kCATransitionReveal]; 325 [animation setSubtype:kCATransitionFromRight]; 326 [animation setFillMode:kCAFillModeForwards]; 327 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 328 329 [view.layer addAnimation:animation forKey:nil]; 330 } 331 332 333 + (void)animationEaseIn:(UIView *)view 334 { 335 CATransition *animation = [CATransition animation]; 336 [animation setDuration:0.35f]; 337 [animation setType:kCATransitionFade]; 338 [animation setFillMode:kCAFillModeForwards]; 339 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 340 341 [view.layer addAnimation:animation forKey:nil]; 342 } 343 344 + (void)animationEaseOut:(UIView *)view 345 { 346 CATransition *animation = [CATransition animation]; 347 [animation setDuration:0.35f]; 348 [animation setType:kCATransitionFade]; 349 [animation setFillMode:kCAFillModeForwards]; 350 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 351 352 [view.layer addAnimation:animation forKey:nil]; 353 } 354 355 356 /** 357 * UIViewAnimation 358 * 359 * @see http://www.cocoachina.com/bbs/read.php?tid=110168 360 * 361 * @brief UIView動畫應該是最簡單便捷創建動畫的方式了,詳解請猛戳URL. 362 * 363 * @method beginAnimations:context 第一個參數用來作為動畫的標識,第二個參數給代理代理傳遞消息.至于為什么一個使用 364 * nil而另外一個使用NULL,是因為第一個參數是一個對象指針,而第二個參數是基本數據類型. 365 * @method setAnimationCurve: 設置動畫的加速或減速的方式(速度) 366 * @method setAnimationDuration: 動畫持續時間 367 * @method setAnimationTransition:forView:cache: 第一個參數定義動畫類型,第二個參數是當前視圖對象,第三個參數是是否使用緩沖區 368 * @method commitAnimations 動畫結束 369 */ 370 371 + (void)animationFlipFromLeft:(UIView *)view 372 { 373 [UIView beginAnimations:nil context:NULL]; 374 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 375 [UIView setAnimationDuration:0.35f]; 376 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO]; 377 [UIView commitAnimations]; 378 } 379 380 + (void)animationFlipFromRigh:(UIView *)view 381 { 382 [UIView beginAnimations:nil context:NULL]; 383 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 384 [UIView setAnimationDuration:0.35f]; 385 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO]; 386 [UIView commitAnimations]; 387 } 388 389 390 + (void)animationCurlUp:(UIView *)view 391 { 392 [UIView beginAnimations:nil context:NULL]; 393 [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 394 [UIView setAnimationDuration:0.35f]; 395 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO]; 396 [UIView commitAnimations]; 397 } 398 399 + (void)animationCurlDown:(UIView *)view 400 { 401 [UIView beginAnimations:nil context:NULL]; 402 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 403 [UIView setAnimationDuration:0.35f]; 404 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO]; 405 [UIView commitAnimations]; 406 } 407 408 + (void)animationPushUp:(UIView *)view 409 { 410 CATransition *animation = [CATransition animation]; 411 [animation setDuration:0.35f]; 412 [animation setFillMode:kCAFillModeForwards]; 413 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 414 [animation setType:kCATransitionPush]; 415 [animation setSubtype:kCATransitionFromTop]; 416 417 [view.layer addAnimation:animation forKey:nil]; 418 } 419 420 + (void)animationPushDown:(UIView *)view 421 { 422 CATransition *animation = [CATransition animation]; 423 [animation setDuration:0.35f]; 424 [animation setFillMode:kCAFillModeForwards]; 425 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 426 [animation setType:kCATransitionPush]; 427 [animation setSubtype:kCATransitionFromBottom]; 428 429 [view.layer addAnimation:animation forKey:nil]; 430 } 431 432 + (void)animationPushLeft:(UIView *)view 433 { 434 CATransition *animation = [CATransition animation]; 435 [animation setDuration:0.35f]; 436 [animation setFillMode:kCAFillModeForwards]; 437 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 438 [animation setType:kCATransitionPush]; 439 [animation setSubtype:kCATransitionFromLeft]; 440 441 [view.layer addAnimation:animation forKey:nil]; 442 } 443 444 + (void)animationPushRight:(UIView *)view 445 { 446 CATransition *animation = [CATransition animation]; 447 [animation setDuration:0.35f]; 448 [animation setFillMode:kCAFillModeForwards]; 449 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 450 [animation setType:kCATransitionPush]; 451 [animation setSubtype:kCATransitionFromRight]; 452 453 [view.layer addAnimation:animation forKey:nil]; 454 } 455 456 // presentModalViewController 457 + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration 458 { 459 CATransition *animation = [CATransition animation]; 460 [animation setDuration:duration]; 461 [animation setFillMode:kCAFillModeForwards]; 462 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 463 [animation setType:kCATransitionMoveIn]; 464 [animation setSubtype:kCATransitionFromTop]; 465 466 [view.layer addAnimation:animation forKey:nil]; 467 } 468 469 // dissModalViewController 470 + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration 471 { 472 CATransition *transition = [CATransition animation]; 473 transition.duration =0.4; 474 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 475 transition.type = kCATransitionReveal; 476 transition.subtype = kCATransitionFromBottom; 477 [view.layer addAnimation:transition forKey:nil]; 478 } 479 480 + (void)animationMoveLeft:(UIView *)view 481 { 482 CATransition *animation = [CATransition animation]; 483 [animation setDuration:0.35f]; 484 [animation setFillMode:kCAFillModeForwards]; 485 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 486 [animation setType:kCATransitionMoveIn]; 487 [animation setSubtype:kCATransitionFromLeft]; 488 489 [view.layer addAnimation:animation forKey:nil]; 490 } 491 492 + (void)animationMoveRight:(UIView *)view 493 { 494 CATransition *animation = [CATransition animation]; 495 [animation setDuration:0.35f]; 496 [animation setFillMode:kCAFillModeForwards]; 497 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 498 [animation setType:kCATransitionMoveIn]; 499 [animation setSubtype:kCATransitionFromRight]; 500 501 [view.layer addAnimation:animation forKey:nil]; 502 } 503 504 +(void)animationRotateAndScaleEffects:(UIView *)view 505 { 506 [UIView animateWithDuration:0.35f animations:^ 507 { 508 /** 509 * @see http://donbe.blog.163.com/blog/static/138048021201061054243442/ 510 * 511 * @param transform 形變屬性(結構體),可以利用這個屬性去對view做一些翻轉或者縮放.詳解請猛戳↑URL. 512 * 513 * @method valueWithCATransform3D: 此方法需要一個CATransform3D的結構體.一些非詳細的講解可以看下面的URL 514 * 515 * @see http://blog.csdn.net/liubo0_0/article/details/7452166 516 * 517 */ 518 519 view.transform = CGAffineTransformMakeScale(0.001, 0.001); 520 521 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 522 523 // 向右旋轉45°縮小到最小,然后再從小到大推出. 524 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)]; 525 526 /** 527 * 其他效果: 528 * 從底部向上收縮一半后彈出 529 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)]; 530 * 531 * 從底部向上完全收縮后彈出 532 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)]; 533 * 534 * 左旋轉45°縮小到最小,然后再從小到大推出. 535 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)]; 536 * 537 * 旋轉180°縮小到最小,然后再從小到大推出. 538 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)]; 539 */ 540 541 animation.duration = 0.45; 542 animation.repeatCount = 1; 543 [view.layer addAnimation:animation forKey:nil]; 544 545 } 546 completion:^(BOOL finished) 547 { 548 [UIView animateWithDuration:0.35f animations:^ 549 { 550 view.transform = CGAffineTransformMakeScale(1.0, 1.0); 551 }]; 552 }]; 553 } 554 555 /** CABasicAnimation 556 * 557 * @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html 558 * 559 * @brief 便利構造函數 animationWithKeyPath: KeyPath需要一個字符串類型的參數,實際上是一個 560 * 鍵-值編碼協議的擴展,參數必須是CALayer的某一項屬性,你的代碼會對應的去改變該屬性的效果 561 * 具體可以填寫什么請參考上面的URL,切勿亂填! 562 * 例如這里填寫的是 @"transform.rotation.z" 意思就是圍繞z軸旋轉,旋轉的單位是弧度. 563 * 這個動畫的效果是把view旋轉到最小,再旋轉回來. 564 * 你也可以填寫@"opacity" 去修改透明度...以此類推.修改layer的屬性,可以用這個類. 565 * 566 * @param toValue 動畫結束的值.CABasicAnimation自己只有三個屬性(都很重要)(其他屬性是繼承來的),分別為: 567 * fromValue(開始值), toValue(結束值), byValue(偏移值), 568 ! 這三個屬性最多只能同時設置兩個; 569 * 他們之間的關系如下: 570 * 如果同時設置了fromValue和toValue,那么動畫就會從fromValue過渡到toValue; 571 * 如果同時設置了fromValue和byValue,那么動畫就會從fromValue過渡到fromValue + byValue; 572 * 如果同時設置了byValue 和toValue,那么動畫就會從toValue - byValue過渡到toValue; 573 * 574 * 如果只設置了fromValue,那么動畫就會從fromValue過渡到當前的value; 575 * 如果只設置了toValue ,那么動畫就會從當前的value過渡到toValue; 576 * 如果只設置了byValue ,那么動畫就會從從當前的value過渡到當前value + byValue. 577 * 578 * 可以這么理解,當你設置了三個中的一個或多個,系統就會根據以上規則使用插值算法計算出一個時間差并 579 * 同時開啟一個Timer.Timer的間隔也就是這個時間差,通過這個Timer去不停地刷新keyPath的值. 580 ! 而實際上,keyPath的值(layer的屬性)在動畫運行這一過程中,是沒有任何變化的,它只是調用了GPU去 581 * 完成這些顯示效果而已. 582 * 在這個動畫里,是設置了要旋轉到的弧度,根據以上規則,動畫將會從它當前的弧度專旋轉到我設置的弧度. 583 * 584 * @param duration 動畫持續時間 585 * 586 * @param timingFunction 動畫起點和終點之間的插值計算,也就是說它決定了動畫運行的節奏,是快還是慢,還是先快后慢... 587 */ 588 589 /** CAAnimationGroup 590 * 591 * @brief 顧名思義,這是一個動畫組,它允許多個動畫組合在一起并行顯示.比如這里設置了兩個動畫, 592 * 把他們加在動畫組里,一起顯示.例如你有幾個動畫,在動畫執行的過程中需要同時修改動畫的某些屬性, 593 * 這時候就可以使用CAAnimationGroup. 594 * 595 * @param duration 動畫持續時間,值得一提的是,如果添加到group里的子動畫不設置此屬性,group里的duration會統一 596 * 設置動畫(包括子動畫)的duration屬性;但是如果子動畫設置了duration屬性,那么group的duration屬性 597 * 的值不應該小于每個子動畫中duration屬性的值,否則會造成子動畫顯示不全就停止了動畫. 598 * 599 * @param autoreverses 動畫完成后自動重新開始,默認為NO. 600 * 601 * @param repeatCount 動畫重復次數,默認為0. 602 * 603 * @param animations 動畫組(數組類型),把需要同時運行的動畫加到這個數組里. 604 * 605 * @note addAnimation:forKey 這個方法的forKey參數是一個字符串,這個字符串可以隨意設置. 606 * 607 * @note 如果你需要在動畫group執行結束后保存動畫效果的話,設置 fillMode 屬性,并且把 608 * removedOnCompletion 設置為NO; 609 */ 610 611 + (void)animationRotateAndScaleDownUp:(UIView *)view 612 { 613 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 614 rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2]; 615 rotationAnimation.duration = 0.35f; 616 rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 617 618 CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 619 scaleAnimation.toValue = [NSNumber numberWithFloat:0.0]; 620 scaleAnimation.duration = 0.35f; 621 scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 622 623 CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 624 animationGroup.duration = 0.35f; 625 animationGroup.autoreverses = YES; 626 animationGroup.repeatCount = 1; 627 animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil]; 628 [view.layer addAnimation:animationGroup forKey:@"animationGroup"]; 629 } 630 631 632 633 #pragma mark - Private API 634 635 + (void)animationFlipFromTop:(UIView *)view 636 { 637 CATransition *animation = [CATransition animation]; 638 [animation setDuration:0.35f]; 639 [animation setFillMode:kCAFillModeForwards]; 640 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 641 [animation setType:@"oglFlip"]; 642 [animation setSubtype:@"fromTop"]; 643 644 [view.layer addAnimation:animation forKey:nil]; 645 } 646 647 + (void)animationFlipFromBottom:(UIView *)view 648 { 649 CATransition *animation = [CATransition animation]; 650 [animation setDuration:0.35f]; 651 [animation setFillMode:kCAFillModeForwards]; 652 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 653 [animation setType:@"oglFlip"]; 654 [animation setSubtype:@"fromBottom"]; 655 656 [view.layer addAnimation:animation forKey:nil]; 657 } 658 659 + (void)animationCubeFromLeft:(UIView *)view 660 { 661 CATransition *animation = [CATransition animation]; 662 [animation setDuration:0.35f]; 663 [animation setFillMode:kCAFillModeForwards]; 664 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 665 [animation setType:@"cube"]; 666 [animation setSubtype:@"fromLeft"]; 667 668 [view.layer addAnimation:animation forKey:nil]; 669 } 670 671 + (void)animationCubeFromRight:(UIView *)view 672 { 673 CATransition *animation = [CATransition animation]; 674 [animation setDuration:0.35f]; 675 [animation setFillMode:kCAFillModeForwards]; 676 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 677 [animation setType:@"cube"]; 678 [animation setSubtype:@"fromRight"]; 679 680 [view.layer addAnimation:animation forKey:nil]; 681 } 682 683 + (void)animationCubeFromTop:(UIView *)view 684 { 685 CATransition *animation = [CATransition animation]; 686 [animation setDuration:0.35f]; 687 [animation setFillMode:kCAFillModeForwards]; 688 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 689 [animation setType:@"cube"]; 690 [animation setSubtype:@"fromTop"]; 691 692 [view.layer addAnimation:animation forKey:nil]; 693 } 694 695 + (void)animationCubeFromBottom:(UIView *)view 696 { 697 CATransition *animation = [CATransition animation]; 698 [animation setDuration:0.35f]; 699 [animation setFillMode:kCAFillModeForwards]; 700 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 701 [animation setType:@"cube"]; 702 [animation setSubtype:@"fromBottom"]; 703 704 [view.layer addAnimation:animation forKey:nil]; 705 } 706 707 + (void)animationSuckEffect:(UIView *)view 708 { 709 CATransition *animation = [CATransition animation]; 710 [animation setDuration:0.35f]; 711 [animation setFillMode:kCAFillModeForwards]; 712 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 713 [animation setType:@"suckEffect"]; 714 715 [view.layer addAnimation:animation forKey:nil]; 716 } 717 718 + (void)animationRippleEffect:(UIView *)view 719 { 720 CATransition *animation = [CATransition animation]; 721 [animation setDuration:0.35f]; 722 [animation setFillMode:kCAFillModeForwards]; 723 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 724 [animation setType:@"rippleEffect"]; 725 726 [view.layer addAnimation:animation forKey:nil]; 727 } 728 729 + (void)animationCameraOpen:(UIView *)view 730 { 731 CATransition *animation = [CATransition animation]; 732 [animation setDuration:0.35f]; 733 [animation setFillMode:kCAFillModeForwards]; 734 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 735 [animation setType:@"cameraIrisHollowOpen"]; 736 [animation setSubtype:@"fromRight"]; 737 738 [view.layer addAnimation:animation forKey:nil]; 739 } 740 741 + (void)animationCameraClose:(UIView *)view 742 { 743 CATransition *animation = [CATransition animation]; 744 [animation setDuration:0.35f]; 745 [animation setFillMode:kCAFillModeForwards]; 746 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 747 [animation setType:@"cameraIrisHollowClose"]; 748 [animation setSubtype:@"fromRight"]; 749 750 [view.layer addAnimation:animation forKey:nil]; 751 } 752 @end?
轉載于:https://www.cnblogs.com/pretty-guy/articles/4013839.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: CF1030F Putting Boxe
- 下一篇: C# winform 捕获全局异常