原文: http://blog.csdn.net/zhibudefeng/article/details/8691567php
CoreAnimationEffect.h 文件
1 // CoreAnimationEffect.h 2 3 // CoreAnimationEffect 4 5 // 6 7 // Created by VincentXue on 13-1-19. 8 9 // Copyright (c) 2013年 VincentXue. All rights reserved. 10 11 // 12 13 #import <Foundation/Foundation.h> 14 15 /** 16 17 ! 導入QuartzCore.framework 18 19 * 20 21 * Example: 22 23 * 24 25 * Step.1 26 27 * 28 29 * #import "CoreAnimationEffect.h" 30 31 * 32 33 * Step.2 34 35 * 36 37 * [CoreAnimationEffect animationMoveLeft:your view]; 38 39 * 40 41 */ 42 43 @interface CoreAnimationEffect : NSObject 44 45 #pragma mark - Custom Animation 46 47 /** 48 49 * @brief 快速構建一個你自定義的動畫,有如下參數供你設置. 50 51 * 52 53 * @note 調用系統預置Type須要在調用類引入下句 54 55 * 56 57 * #import <QuartzCore/QuartzCore.h> 58 59 * 60 61 * @param type 動畫過渡類型 62 63 * @param subType 動畫過渡方向(子類型) 64 65 * @param duration 動畫持續時間 66 67 * @param timingFunction 動畫定時函數屬性 68 69 * @param theView 須要添加動畫的view. 70 71 * 72 73 * 74 75 */ 76 77 + (void)showAnimationType:(NSString *)type 78 79 withSubType:(NSString *)subType 80 81 duration:(CFTimeInterval)duration 82 83 timingFunction:(NSString *)timingFunction 84 85 view:(UIView *)theView; 86 87 #pragma mark - Preset Animation 88 89 /** 90 91 * 下面是一些經常使用的動畫效果 92 93 */ 94 95 // reveal 96 97 + (void)animationRevealFromBottom:(UIView *)view; 98 99 + (void)animationRevealFromTop:(UIView *)view; 100 101 + (void)animationRevealFromLeft:(UIView *)view; 102 103 + (void)animationRevealFromRight:(UIView *)view; 104 105 // 漸隱漸消 106 107 + (void)animationEaseIn:(UIView *)view; 108 109 + (void)animationEaseOut:(UIView *)view; 110 111 // 翻轉 112 113 + (void)animationFlipFromLeft:(UIView *)view; 114 115 + (void)animationFlipFromRigh:(UIView *)view; 116 117 // 翻頁 118 119 + (void)animationCurlUp:(UIView *)view; 120 121 + (void)animationCurlDown:(UIView *)view; 122 123 // push 124 125 + (void)animationPushUp:(UIView *)view; 126 127 + (void)animationPushDown:(UIView *)view; 128 129 + (void)animationPushLeft:(UIView *)view; 130 131 + (void)animationPushRight:(UIView *)view; 132 133 // move 134 135 + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration; 136 137 + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration; 138 139 + (void)animationMoveLeft:(UIView *)view; 140 141 + (void)animationMoveRight:(UIView *)view; 142 143 // 旋轉縮放 144 145 // 各類旋轉縮放效果 146 147 + (void)animationRotateAndScaleEffects:(UIView *)view; 148 149 // 旋轉同時縮小放大效果 150 151 + (void)animationRotateAndScaleDownUp:(UIView *)view; 152 153 #pragma mark - Private API 154 155 /** 156 157 * 下面動畫裏用到的某些屬性在當前API裏是不合法的,可是也能夠用. 158 159 */ 160 161 + (void)animationFlipFromTop:(UIView *)view; 162 163 + (void)animationFlipFromBottom:(UIView *)view; 164 165 + (void)animationCubeFromLeft:(UIView *)view; 166 167 + (void)animationCubeFromRight:(UIView *)view; 168 169 + (void)animationCubeFromTop:(UIView *)view; 170 171 + (void)animationCubeFromBottom:(UIView *)view; 172 173 + (void)animationSuckEffect:(UIView *)view; 174 175 + (void)animationRippleEffect:(UIView *)view; 176 177 + (void)animationCameraOpen:(UIView *)view; 178 179 + (void)animationCameraClose:(UIView *)view; 180 181 @end
CoreAnimationEffect.m 文件
1 // CoreAnimationEffect.m 2 3 // CoreAnimationEffect 4 5 // 6 7 // Created by VincentXue on 13-1-19. 8 9 // Copyright (c) 2013年 VincentXue. All rights reserved. 10 11 // 12 13 #import "CoreAnimationEffect.h" 14 15 #import <QuartzCore/QuartzCore.h> 16 17 @implementation CoreAnimationEffect 18 19 /** 20 21 * 首先推薦一個不錯的網站. http://www.raywenderlich.com 22 23 */ 24 25 #pragma mark - Custom Animation 26 27 + (void)showAnimationType:(NSString *)type 28 29 withSubType:(NSString *)subType 30 31 duration:(CFTimeInterval)duration 32 33 timingFunction:(NSString *)timingFunction 34 35 view:(UIView *)theView 36 37 { 38 39 /** CATransition 40 41 * 42 43 * @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html 44 45 * @see http://geeklu.com/2012/09/animation-in-ios/ 46 47 * 48 49 * CATransition 經常使用設置及屬性註解以下: 50 51 */ 52 53 CATransition *animation = [CATransition animation]; 54 55 /** delegate 56 57 * 58 59 * 動畫的代理,若是你想在動畫開始和結束的時候作一些事,能夠設置此屬性,它會自動回調兩個代理方法. 60 61 * 62 63 * @see CAAnimationDelegate (按下command鍵點擊) 64 65 */ 66 67 animation.delegate = self; 68 69 /** duration 70 71 * 72 73 * 動畫持續時間 74 75 */ 76 77 animation.duration = duration; 78 79 /** timingFunction 80 81 * 82 83 * 用於變化起點和終點之間的插值計算,形象點說它決定了動畫運行的節奏,好比是均勻變化(相同時間變化量相同)仍是 84 85 * 先快後慢,先慢後快仍是先慢再快再慢. 86 87 * 88 89 * 動畫的開始與結束的快慢,有五個預置分別爲(下同): 90 91 * kCAMediaTimingFunctionLinear 線性,即勻速 92 93 * kCAMediaTimingFunctionEaseIn 先慢後快 94 95 * kCAMediaTimingFunctionEaseOut 先快後慢 96 97 * kCAMediaTimingFunctionEaseInEaseOut 先慢後快再慢 98 99 * kCAMediaTimingFunctionDefault 實際效果是動畫中間比較快. 100 101 */ 102 103 /** timingFunction 104 105 * 106 107 * 當上面的預置不能知足你的需求的時候,你可使用下面的兩個方法來自定義你的timingFunction 108 109 * 具體參見下面的URL 110 111 * 112 113 * @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html 114 115 * 116 117 * + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 118 119 * 120 121 * - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 122 123 */ 124 125 animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction]; 126 127 /** fillMode 128 129 * 130 131 * 決定當前對象過了非active時間段的行爲,好比動畫開始以前,動畫結束以後. 132 133 * 預置爲: 134 135 * kCAFillModeRemoved 默認,當動畫開始前和動畫結束後,動畫對layer都沒有影響,動畫結束後,layer會恢復到以前的狀態 136 137 * kCAFillModeForwards 當動畫結束後,layer會一直保持着動畫最後的狀態 138 139 * kCAFillModeBackwards 和kCAFillModeForwards相對,具體參考上面的URL 140 141 * kCAFillModeBoth kCAFillModeForwards和kCAFillModeBackwards在一塊兒的效果 142 143 */ 144 145 animation.fillMode = kCAFillModeForwards; 146 147 /** removedOnCompletion 148 149 * 150 151 * 這個屬性默認爲YES.通常狀況下,不須要設置這個屬性. 152 153 * 154 155 * 但若是是CAAnimation動畫,而且須要設置 fillMode 屬性,那麼須要將 removedOnCompletion 設置爲NO,不然 156 157 * fillMode無效 158 159 */ 160 161 // animation.removedOnCompletion = NO; 162 163 /** type 164 165 * 166 167 * 各類動畫效果 其中除了'fade', `moveIn', `push' , `reveal' ,其餘屬於似有的API(我是這麼認爲的,能夠點進去看下注釋). 168 169 * ↑↑↑上面四個能夠分別使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'來調用. 170 171 * @"cube" 立方體翻滾效果 172 173 * @"moveIn" 新視圖移到舊視圖上面 174 175 * @"reveal" 顯露效果(將舊視圖移開,顯示下面的新視圖) 176 177 * @"fade" 交叉淡化過渡(不支持過渡方向) (默認爲此效果) 178 179 * @"pageCurl" 向上翻一頁 180 181 * @"pageUnCurl" 向下翻一頁 182 183 * @"suckEffect" 收縮效果,相似系統最小化窗口時的神奇效果(不支持過渡方向) 184 185 * @"rippleEffect" 滴水效果,(不支持過渡方向) 186 187 * @"oglFlip" 上下左右翻轉效果 188 189 * @"rotate" 旋轉效果 190 191 * @"push" 192 193 * @"cameraIrisHollowOpen" 相機鏡頭打開效果(不支持過渡方向) 194 195 * @"cameraIrisHollowClose" 相機鏡頭關上效果(不支持過渡方向) 196 197 */ 198 199 /** type 200 201 * 202 203 * kCATransitionFade 交叉淡化過渡 204 205 * kCATransitionMoveIn 新視圖移到舊視圖上面 206 207 * kCATransitionPush 新視圖把舊視圖推出去 208 209 * kCATransitionReveal 將舊視圖移開,顯示下面的新視圖 210 211 */ 212 213 animation.type = type; 214 215 /** subtype 216 217 * 218 219 * 各類動畫方向 220 221 * 222 223 * kCATransitionFromRight; 同字面意思(下同) 224 225 * kCATransitionFromLeft; 226 227 * kCATransitionFromTop; 228 229 * kCATransitionFromBottom; 230 231 */ 232 233 /** subtype 234 235 * 236 237 * 當type爲@"rotate"(旋轉)的時候,它也有幾個對應的subtype,分別爲: 238 239 * 90cw 逆時針旋轉90° 240 241 * 90ccw 順時針旋轉90° 242 243 * 180cw 逆時針旋轉180° 244 245 * 180ccw 順時針旋轉180° 246 247 */ 248 249 /** 250 251 * type與subtype的對應關係(必看),若是對應錯誤,動畫不會顯現. 252 253 * 254 255 * @see http://iphonedevwiki.net/index.php/CATransition 256 257 */ 258 259 animation.subtype = subType; 260 261 /** 262 263 * 全部核心動畫和特效都是基於CAAnimation,而CAAnimation是做用於CALayer的.因此把動畫添加到layer上. 264 265 * forKey 能夠是任意字符串. 266 267 */ 268 269 [theView.layer addAnimation:animation forKey:nil]; 270 271 } 272 273 #pragma mark - Preset Animation 274 275 + (void)animationRevealFromBottom:(UIView *)view 276 277 { 278 279 CATransition *animation = [CATransition animation]; 280 281 [animation setDuration:0.35f]; 282 283 [animation setType:kCATransitionReveal]; 284 285 [animation setSubtype:kCATransitionFromBottom]; 286 287 [animation setFillMode:kCAFillModeForwards]; 288 289 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 290 291 [view.layer addAnimation:animation forKey:nil]; 292 293 } 294 295 + (void)animationRevealFromTop:(UIView *)view 296 297 { 298 299 CATransition *animation = [CATransition animation]; 300 301 [animation setDuration:0.35f]; 302 303 [animation setType:kCATransitionReveal]; 304 305 [animation setSubtype:kCATransitionFromTop]; 306 307 [animation setFillMode:kCAFillModeForwards]; 308 309 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 310 311 [view.layer addAnimation:animation forKey:nil]; 312 313 } 314 315 + (void)animationRevealFromLeft:(UIView *)view 316 317 { 318 319 CATransition *animation = [CATransition animation]; 320 321 [animation setDuration:0.35f]; 322 323 [animation setType:kCATransitionReveal]; 324 325 [animation setSubtype:kCATransitionFromLeft]; 326 327 [animation setFillMode:kCAFillModeForwards]; 328 329 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 330 331 [view.layer addAnimation:animation forKey:nil]; 332 333 } 334 335 + (void)animationRevealFromRight:(UIView *)view 336 337 { 338 339 CATransition *animation = [CATransition animation]; 340 341 [animation setDuration:0.35f]; 342 343 [animation setType:kCATransitionReveal]; 344 345 [animation setSubtype:kCATransitionFromRight]; 346 347 [animation setFillMode:kCAFillModeForwards]; 348 349 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 350 351 [view.layer addAnimation:animation forKey:nil]; 352 353 } 354 355 + (void)animationEaseIn:(UIView *)view 356 357 { 358 359 CATransition *animation = [CATransition animation]; 360 361 [animation setDuration:0.35f]; 362 363 [animation setType:kCATransitionFade]; 364 365 [animation setFillMode:kCAFillModeForwards]; 366 367 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; 368 369 [view.layer addAnimation:animation forKey:nil]; 370 371 } 372 373 + (void)animationEaseOut:(UIView *)view 374 375 { 376 377 CATransition *animation = [CATransition animation]; 378 379 [animation setDuration:0.35f]; 380 381 [animation setType:kCATransitionFade]; 382 383 [animation setFillMode:kCAFillModeForwards]; 384 385 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 386 387 [view.layer addAnimation:animation forKey:nil]; 388 389 } 390 391 /** 392 393 * UIViewAnimation 394 395 * 396 397 * @see http://www.cocoachina.com/bbs/read.php?tid=110168 398 399 * 400 401 * @brief UIView動畫應該是最簡單便捷建立動畫的方式了,詳解請猛戳URL. 402 403 * 404 405 * @method beginAnimations:context 第一個參數用來做爲動畫的標識,第二個參數給代理代理傳遞消息.至於爲何一個使用 406 407 * nil而另一個使用NULL,是由於第一個參數是一個對象指針,而第二個參數是基本數據類型. 408 409 * @method setAnimationCurve: 設置動畫的加速或減速的方式(速度) 410 411 * @method setAnimationDuration: 動畫持續時間 412 413 * @method setAnimationTransition:forView:cache: 第一個參數定義動畫類型,第二個參數是當前視圖對象,第三個參數是是否使用緩衝區 414 415 * @method commitAnimations 動畫結束 416 417 */ 418 419 + (void)animationFlipFromLeft:(UIView *)view 420 421 { 422 423 [UIView beginAnimations:nil context:NULL]; 424 425 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 426 427 [UIView setAnimationDuration:0.35f]; 428 429 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO]; 430 431 [UIView commitAnimations]; 432 433 } 434 435 + (void)animationFlipFromRigh:(UIView *)view 436 437 { 438 439 [UIView beginAnimations:nil context:NULL]; 440 441 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 442 443 [UIView setAnimationDuration:0.35f]; 444 445 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view cache:NO]; 446 447 [UIView commitAnimations]; 448 449 } 450 451 + (void)animationCurlUp:(UIView *)view 452 453 { 454 455 [UIView beginAnimations:nil context:NULL]; 456 457 [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 458 459 [UIView setAnimationDuration:0.35f]; 460 461 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO]; 462 463 [UIView commitAnimations]; 464 465 } 466 467 + (void)animationCurlDown:(UIView *)view 468 469 { 470 471 [UIView beginAnimations:nil context:NULL]; 472 473 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 474 475 [UIView setAnimationDuration:0.35f]; 476 477 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO]; 478 479 [UIView commitAnimations]; 480 481 } 482 483 + (void)animationPushUp:(UIView *)view 484 485 { 486 487 CATransition *animation = [CATransition animation]; 488 489 [animation setDuration:0.35f]; 490 491 [animation setFillMode:kCAFillModeForwards]; 492 493 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 494 495 [animation setType:kCATransitionPush]; 496 497 [animation setSubtype:kCATransitionFromTop]; 498 499 [view.layer addAnimation:animation forKey:nil]; 500 501 } 502 503 + (void)animationPushDown:(UIView *)view 504 505 { 506 507 CATransition *animation = [CATransition animation]; 508 509 [animation setDuration:0.35f]; 510 511 [animation setFillMode:kCAFillModeForwards]; 512 513 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 514 515 [animation setType:kCATransitionPush]; 516 517 [animation setSubtype:kCATransitionFromBottom]; 518 519 [view.layer addAnimation:animation forKey:nil]; 520 521 } 522 523 + (void)animationPushLeft:(UIView *)view 524 525 { 526 527 CATransition *animation = [CATransition animation]; 528 529 [animation setDuration:0.35f]; 530 531 [animation setFillMode:kCAFillModeForwards]; 532 533 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 534 535 [animation setType:kCATransitionPush]; 536 537 [animation setSubtype:kCATransitionFromLeft]; 538 539 [view.layer addAnimation:animation forKey:nil]; 540 541 } 542 543 + (void)animationPushRight:(UIView *)view 544 545 { 546 547 CATransition *animation = [CATransition animation]; 548 549 [animation setDuration:0.35f]; 550 551 [animation setFillMode:kCAFillModeForwards]; 552 553 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 554 555 [animation setType:kCATransitionPush]; 556 557 [animation setSubtype:kCATransitionFromRight]; 558 559 [view.layer addAnimation:animation forKey:nil]; 560 561 } 562 563 // presentModalViewController 564 565 + (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration 566 567 { 568 569 CATransition *animation = [CATransition animation]; 570 571 [animation setDuration:duration]; 572 573 [animation setFillMode:kCAFillModeForwards]; 574 575 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 576 577 [animation setType:kCATransitionMoveIn]; 578 579 [animation setSubtype:kCATransitionFromTop]; 580 581 [view.layer addAnimation:animation forKey:nil]; 582 583 } 584 585 // dissModalViewController 586 587 + (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration 588 589 { 590 591 CATransition *transition = [CATransition animation]; 592 593 transition.duration =0.4; 594 595 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 596 597 transition.type = kCATransitionReveal; 598 599 transition.subtype = kCATransitionFromBottom; 600 601 [view.layer addAnimation:transition forKey:nil]; 602 603 } 604 605 + (void)animationMoveLeft:(UIView *)view 606 607 { 608 609 CATransition *animation = [CATransition animation]; 610 611 [animation setDuration:0.35f]; 612 613 [animation setFillMode:kCAFillModeForwards]; 614 615 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 616 617 [animation setType:kCATransitionMoveIn]; 618 619 [animation setSubtype:kCATransitionFromLeft]; 620 621 [view.layer addAnimation:animation forKey:nil]; 622 623 } 624 625 + (void)animationMoveRight:(UIView *)view 626 627 { 628 629 CATransition *animation = [CATransition animation]; 630 631 [animation setDuration:0.35f]; 632 633 [animation setFillMode:kCAFillModeForwards]; 634 635 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 636 637 [animation setType:kCATransitionMoveIn]; 638 639 [animation setSubtype:kCATransitionFromRight]; 640 641 [view.layer addAnimation:animation forKey:nil]; 642 643 } 644 645 +(void)animationRotateAndScaleEffects:(UIView *)view 646 647 { 648 649 [UIView animateWithDuration:0.35f animations:^ 650 651 { 652 653 /** 654 655 * @see http://donbe.blog.163.com/blog/static/138048021201061054243442/ 656 657 * 658 659 * @param transform 形變屬性(結構體),能夠利用這個屬性去對view作一些翻轉或者縮放.詳解請猛戳↑URL. 660 661 * 662 663 * @method valueWithCATransform3D: 此方法須要一個CATransform3D的結構體.一些非詳細的講解能夠看下面的URL 664 665 * 666 667 * @see http://blog.csdn.net/liubo0_0/article/details/7452166 668 669 * 670 671 */ 672 673 view.transform = CGAffineTransformMakeScale(0.001, 0.001); 674 675 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"]; 676 677 // 向右旋轉45°縮小到最小,而後再從小到大推出. 678 679 animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)]; 680 681 /** 682 683 * 其餘效果: 684 685 * 從底部向上收縮一半後彈出 686 687 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)]; 688 689 * 690 691 * 從底部向上徹底收縮後彈出 692 693 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)]; 694 695 * 696 697 * 左旋轉45°縮小到最小,而後再從小到大推出. 698 699 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)]; 700 701 * 702 703 * 旋轉180°縮小到最小,而後再從小到大推出. 704 705 * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)]; 706 707 */ 708 709 animation.duration = 0.45; 710 711 animation.repeatCount = 1; 712 713 [view.layer addAnimation:animation forKey:nil]; 714 715 } 716 717 completion:^(BOOL finished) 718 719 { 720 721 [UIView animateWithDuration:0.35f animations:^ 722 723 { 724 725 view.transform = CGAffineTransformMakeScale(1.0, 1.0); 726 727 }]; 728 729 }]; 730 731 } 732 733 /** CABasicAnimation 734 735 * 736 737 * @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html 738 739 * 740 741 * @brief 便利構造函數 animationWithKeyPath: KeyPath須要一個字符串類型的參數,其實是一個 742 743 * 鍵-值編碼協議的擴展,參數必須是CALayer的某一項屬性,你的代碼會對應的去改變該屬性的效果 744 745 * 具體能夠填寫什麼請參考上面的URL,切勿亂填! 746 747 * 例如這裏填寫的是 @"transform.rotation.z" 意思就是圍繞z軸旋轉,旋轉的單位是弧度. 748 749 * 這個動畫的效果是把view旋轉到最小,再旋轉回來. 750 751 * 你也能夠填寫@"opacity" 去修改透明度...以此類推.修改layer的屬性,能夠用這個類. 752 753 * 754 755 * @param toValue 動畫結束的值.CABasicAnimation本身只有三個屬性(都很重要)(其餘屬性是繼承來的),分別爲: 756 757 * fromValue(開始值), toValue(結束值), byValue(偏移值), 758 759 ! 這三個屬性最多隻能同時設置兩個; 760 761 * 他們之間的關係以下: 762 763 * 若是同時設置了fromValue和toValue,那麼動畫就會從fromValue過渡到toValue; 764 765 * 若是同時設置了fromValue和byValue,那麼動畫就會從fromValue過渡到fromValue + byValue; 766 767 * 若是同時設置了byValue 和toValue,那麼動畫就會從toValue - byValue過渡到toValue; 768 769 * 770 771 * 若是隻設置了fromValue,那麼動畫就會從fromValue過渡到當前的value; 772 773 * 若是隻設置了toValue ,那麼動畫就會從當前的value過渡到toValue; 774 775 * 若是隻設置了byValue ,那麼動畫就會從從當前的value過渡到當前value + byValue. 776 777 * 778 779 * 能夠這麼理解,當你設置了三個中的一個或多個,系統就會根據以上規則使用插值算法計算出一個時間差並 780 781 * 同時開啓一個Timer.Timer的間隔也就是這個時間差,經過這個Timer去不停地刷新keyPath的值. 782 783 ! 而實際上,keyPath的值(layer的屬性)在動畫運行這一過程當中,是沒有任何變化的,它只是調用了GPU去 784 785 * 完成這些顯示效果而已. 786 787 * 在這個動畫裏,是設置了要旋轉到的弧度,根據以上規則,動畫將會從它當前的弧度專旋轉到我設置的弧度. 788 789 * 790 791 * @param duration 動畫持續時間 792 793 * 794 795 * @param timingFunction 動畫起點和終點之間的插值計算,也就是說它決定了動畫運行的節奏,是快仍是慢,仍是先快後慢... 796 797 */ 798 799 /** CAAnimationGroup 800 801 * 802 803 * @brief 顧名思義,這是一個動畫組,它容許多個動畫組合在一塊兒並行顯示.好比這裏設置了兩個動畫, 804 805 * 把他們加在動畫組裏,一塊兒顯示.例如你有幾個動畫,在動畫執行的過程當中須要同時修改動畫的某些屬性, 806 807 * 這時候就可使用CAAnimationGroup. 808 809 * 810 811 * @param duration 動畫持續時間,值得一提的是,若是添加到group裏的子動畫不設置此屬性,group裏的duration會統一 812 813 * 設置動畫(包括子動畫)的duration屬性;可是若是子動畫設置了duration屬性,那麼group的duration屬性 814 815 * 的值不該該小於每一個子動畫中duration屬性的值,不然會形成子動畫顯示不全就中止了動畫. 816 817 * 818 819 * @param autoreverses 動畫完成後自動從新開始,默認爲NO. 820 821 * 822 823 * @param repeatCount 動畫重複次數,默認爲0. 824 825 * 826 827 * @param animations 動畫組(數組類型),把須要同時運行的動畫加到這個數組裏. 828 829 * 830 831 * @note addAnimation:forKey 這個方法的forKey參數是一個字符串,這個字符串能夠隨意設置. 832 833 * 834 835 * @note 若是你須要在動畫group執行結束後保存動畫效果的話,設置 fillMode 屬性,而且把 836 837 * removedOnCompletion 設置爲NO; 838 839 */ 840 841 + (void)animationRotateAndScaleDownUp:(UIView *)view 842 843 { 844 845 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 846 847 rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2]; 848 849 rotationAnimation.duration = 0.35f; 850 851 rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 852 853 CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 854 855 scaleAnimation.toValue = [NSNumber numberWithFloat:0.0]; 856 857 scaleAnimation.duration = 0.35f; 858 859 scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 860 861 CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 862 863 animationGroup.duration = 0.35f; 864 865 animationGroup.autoreverses = YES; 866 867 animationGroup.repeatCount = 1; 868 869 animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil]; 870 871 [view.layer addAnimation:animationGroup forKey:@"animationGroup"]; 872 873 } 874 875 #pragma mark - Private API 876 877 + (void)animationFlipFromTop:(UIView *)view 878 879 { 880 881 CATransition *animation = [CATransition animation]; 882 883 [animation setDuration:0.35f]; 884 885 [animation setFillMode:kCAFillModeForwards]; 886 887 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 888 889 [animation setType:@"oglFlip"]; 890 891 [animation setSubtype:@"fromTop"]; 892 893 [view.layer addAnimation:animation forKey:nil]; 894 895 } 896 897 + (void)animationFlipFromBottom:(UIView *)view 898 899 { 900 901 CATransition *animation = [CATransition animation]; 902 903 [animation setDuration:0.35f]; 904 905 [animation setFillMode:kCAFillModeForwards]; 906 907 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 908 909 [animation setType:@"oglFlip"]; 910 911 [animation setSubtype:@"fromBottom"]; 912 913 [view.layer addAnimation:animation forKey:nil]; 914 915 } 916 917 + (void)animationCubeFromLeft:(UIView *)view 918 919 { 920 921 CATransition *animation = [CATransition animation]; 922 923 [animation setDuration:0.35f]; 924 925 [animation setFillMode:kCAFillModeForwards]; 926 927 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 928 929 [animation setType:@"cube"]; 930 931 [animation setSubtype:@"fromLeft"]; 932 933 [view.layer addAnimation:animation forKey:nil]; 934 935 } 936 937 + (void)animationCubeFromRight:(UIView *)view 938 939 { 940 941 CATransition *animation = [CATransition animation]; 942 943 [animation setDuration:0.35f]; 944 945 [animation setFillMode:kCAFillModeForwards]; 946 947 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 948 949 [animation setType:@"cube"]; 950 951 [animation setSubtype:@"fromRight"]; 952 953 [view.layer addAnimation:animation forKey:nil]; 954 955 } 956 957 + (void)animationCubeFromTop:(UIView *)view 958 959 { 960 961 CATransition *animation = [CATransition animation]; 962 963 [animation setDuration:0.35f]; 964 965 [animation setFillMode:kCAFillModeForwards]; 966 967 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 968 969 [animation setType:@"cube"]; 970 971 [animation setSubtype:@"fromTop"]; 972 973 [view.layer addAnimation:animation forKey:nil]; 974 975 } 976 977 + (void)animationCubeFromBottom:(UIView *)view 978 979 { 980 981 CATransition *animation = [CATransition animation]; 982 983 [animation setDuration:0.35f]; 984 985 [animation setFillMode:kCAFillModeForwards]; 986 987 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 988 989 [animation setType:@"cube"]; 990 991 [animation setSubtype:@"fromBottom"]; 992 993 [view.layer addAnimation:animation forKey:nil]; 994 995 } 996 997 + (void)animationSuckEffect:(UIView *)view 998 999 { 1000 1001 CATransition *animation = [CATransition animation]; 1002 1003 [animation setDuration:0.35f]; 1004 1005 [animation setFillMode:kCAFillModeForwards]; 1006 1007 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 1008 1009 [animation setType:@"suckEffect"]; 1010 1011 [view.layer addAnimation:animation forKey:nil]; 1012 1013 } 1014 1015 + (void)animationRippleEffect:(UIView *)view 1016 1017 { 1018 1019 CATransition *animation = [CATransition animation]; 1020 1021 [animation setDuration:0.35f]; 1022 1023 [animation setFillMode:kCAFillModeForwards]; 1024 1025 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 1026 1027 [animation setType:@"rippleEffect"]; 1028 1029 [view.layer addAnimation:animation forKey:nil]; 1030 1031 } 1032 1033 + (void)animationCameraOpen:(UIView *)view 1034 1035 { 1036 1037 CATransition *animation = [CATransition animation]; 1038 1039 [animation setDuration:0.35f]; 1040 1041 [animation setFillMode:kCAFillModeForwards]; 1042 1043 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 1044 1045 [animation setType:@"cameraIrisHollowOpen"]; 1046 1047 [animation setSubtype:@"fromRight"]; 1048 1049 [view.layer addAnimation:animation forKey:nil]; 1050 1051 } 1052 1053 + (void)animationCameraClose:(UIView *)view 1054 1055 { 1056 1057 CATransition *animation = [CATransition animation]; 1058 1059 [animation setDuration:0.35f]; 1060 1061 [animation setFillMode:kCAFillModeForwards]; 1062 1063 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; 1064 1065 [animation setType:@"cameraIrisHollowClose"]; 1066 1067 [animation setSubtype:@"fromRight"]; 1068 1069 [view.layer addAnimation:animation forKey:nil]; 1070 1071 } 1072 1073 @end