转自: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