原文: http://blog.csdn.net/zhibudefeng/article/details/8691567
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