428 | + ( void )animationPushDown:(UIView *)view |
429 | { |
430 | CATransition *animation = [CATransition animation]; |
431 | [animation setDuration:0.35f]; |
432 | [animation setFillMode:kCAFillModeForwards]; |
433 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
434 | [animation setType:kCATransitionPush]; |
435 | [animation setSubtype:kCATransitionFromBottom]; |
436 | |
437 | [view.layer addAnimation:animation forKey:nil]; |
438 | } |
439 | |
440 | + ( void )animationPushLeft:(UIView *)view |
441 | { |
442 | CATransition *animation = [CATransition animation]; |
443 | [animation setDuration:0.35f]; |
444 | [animation setFillMode:kCAFillModeForwards]; |
445 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
446 | [animation setType:kCATransitionPush]; |
447 | [animation setSubtype:kCATransitionFromLeft]; |
448 | |
449 | [view.layer addAnimation:animation forKey:nil]; |
450 | } |
451 | |
452 | + ( void )animationPushRight:(UIView *)view |
453 | { |
454 | CATransition *animation = [CATransition animation]; |
455 | [animation setDuration:0.35f]; |
456 | [animation setFillMode:kCAFillModeForwards]; |
457 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
458 | [animation setType:kCATransitionPush]; |
459 | [animation setSubtype:kCATransitionFromRight]; |
460 | |
461 | [view.layer addAnimation:animation forKey:nil]; |
462 | } |
463 | |
464 | // presentModalViewController |
465 | + ( void )animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration |
466 | { |
467 | CATransition *animation = [CATransition animation]; |
468 | [animation setDuration:duration]; |
469 | [animation setFillMode:kCAFillModeForwards]; |
470 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; |
471 | [animation setType:kCATransitionMoveIn]; |
472 | [animation setSubtype:kCATransitionFromTop]; |
473 | |
474 | [view.layer addAnimation:animation forKey:nil]; |
475 | } |
476 | |
477 | // dissModalViewController |
478 | + ( void )animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration |
479 | { |
480 | CATransition *transition = [CATransition animation]; |
481 | transition.duration =0.4; |
482 | transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; |
483 | transition.type = kCATransitionReveal; |
484 | transition.subtype = kCATransitionFromBottom; |
485 | [view.layer addAnimation:transition forKey:nil]; |
486 | } |
487 | |
488 | + ( void )animationMoveLeft:(UIView *)view |
489 | { |
490 | CATransition *animation = [CATransition animation]; |
491 | [animation setDuration:0.35f]; |
492 | [animation setFillMode:kCAFillModeForwards]; |
493 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
494 | [animation setType:kCATransitionMoveIn]; |
495 | [animation setSubtype:kCATransitionFromLeft]; |
496 | |
497 | [view.layer addAnimation:animation forKey:nil]; |
498 | } |
499 | |
500 | + ( void )animationMoveRight:(UIView *)view |
501 | { |
502 | CATransition *animation = [CATransition animation]; |
503 | [animation setDuration:0.35f]; |
504 | [animation setFillMode:kCAFillModeForwards]; |
505 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
506 | [animation setType:kCATransitionMoveIn]; |
507 | [animation setSubtype:kCATransitionFromRight]; |
508 | |
509 | [view.layer addAnimation:animation forKey:nil]; |
510 | } |
511 | |
512 | +( void )animationRotateAndScaleEffects:(UIView *)view |
513 | { |
514 | [UIView animateWithDuration:0.35f animations:^ |
515 | { |
516 | /** |
517 | * @see http://donbe.blog.163.com/blog/static/138048021201061054243442/ |
518 | * |
519 | * @param transform 形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL. |
520 | * |
521 | * @method valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL |
522 | * |
523 | * @see http://blog.csdn.net/liubo0_0/article/details/7452166 |
524 | * |
525 | */ |
526 | |
527 | view.transform = CGAffineTransformMakeScale(0.001, 0.001); |
528 | |
529 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@ "transform" ]; |
530 | |
531 | // 向右旋转45°缩小到最小,然后再从小到大推出. |
532 | animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)]; |
533 | |
534 | /** |
535 | * 其他效果: |
536 | * 从底部向上收缩一半后弹出 |
537 | * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)]; |
538 | * |
539 | * 从底部向上完全收缩后弹出 |
540 | * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)]; |
541 | * |
542 | * 左旋转45°缩小到最小,然后再从小到大推出. |
543 | * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)]; |
544 | * |
545 | * 旋转180°缩小到最小,然后再从小到大推出. |
546 | * animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)]; |
547 | */ |
548 | |
549 | animation.duration = 0.45; |
550 | animation.repeatCount = 1; |
551 | [view.layer addAnimation:animation forKey:nil]; |
552 | |
553 | } |
554 | completion:^( BOOL finished) |
555 | { |
556 | [UIView animateWithDuration:0.35f animations:^ |
557 | { |
558 | view.transform = CGAffineTransformMakeScale(1.0, 1.0); |
559 | }]; |
560 | }]; |
561 | } |
562 | |
563 | /** CABasicAnimation |
564 | * |
565 | * @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html |
566 | * |
567 | * @brief 便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个 |
568 | * 键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果 |
569 | * 具体可以填写什么请参考上面的URL,切勿乱填! |
570 | * 例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度. |
571 | * 这个动画的效果是把view旋转到最小,再旋转回来. |
572 | * 你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类. |
573 | * |
574 | * @param toValue 动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为: |
575 | * fromValue(开始值), toValue(结束值), byValue(偏移值), |
576 | ! 这三个属性最多只能同时设置两个; |
577 | * 他们之间的关系如下: |
578 | * 如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue; |
579 | * 如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue; |
580 | * 如果同时设置了byValue 和toValue,那么动画就会从toValue - byValue过渡到toValue; |
581 | * |
582 | * 如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value; |
583 | * 如果只设置了toValue ,那么动画就会从当前的value过渡到toValue; |
584 | * 如果只设置了byValue ,那么动画就会从从当前的value过渡到当前value + byValue. |
585 | * |
586 | * 可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并 |
587 | * 同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值. |
588 | ! 而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去 |
589 | * 完成这些显示效果而已. |
590 | * 在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度. |
591 | * |
592 | * @param duration 动画持续时间 |
593 | * |
594 | * @param timingFunction 动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢... |
595 | */ |
596 | |
597 | /** CAAnimationGroup |
598 | * |
599 | * @brief 顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画, |
600 | * 把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性, |
601 | * 这时候就可以使用CAAnimationGroup. |
602 | * |
603 | * @param duration 动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一 |
604 | * 设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性 |
605 | * 的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画. |
606 | * |
607 | * @param autoreverses 动画完成后自动重新开始,默认为NO. |
608 | * |
609 | * @param repeatCount 动画重复次数,默认为0. |
610 | * |
611 | * @param animations 动画组(数组类型),把需要同时运行的动画加到这个数组里. |
612 | * |
613 | * @note addAnimation:forKey 这个方法的forKey参数是一个字符串,这个字符串可以随意设置. |
614 | * |
615 | * @note 如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把 |
616 | * removedOnCompletion 设置为NO; |
617 | */ |
618 | |
619 | + ( void )animationRotateAndScaleDownUp:(UIView *)view |
620 | { |
621 | CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@ "transform.rotation.z" ]; |
622 | rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2]; |
623 | rotationAnimation.duration = 0.35f; |
624 | rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; |
625 | |
626 | CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@ "transform.scale" ]; |
627 | scaleAnimation.toValue = [NSNumber numberWithFloat:0.0]; |
628 | scaleAnimation.duration = 0.35f; |
629 | scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; |
630 | |
631 | CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; |
632 | animationGroup.duration = 0.35f; |
633 | animationGroup.autoreverses = YES; |
634 | animationGroup.repeatCount = 1; |
635 | animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil]; |
636 | [view.layer addAnimation:animationGroup forKey:@ "animationGroup" ]; |
637 | } |
638 | |
639 | |
640 | |
641 | #pragma mark - Private API |
642 | |
643 | + ( void )animationFlipFromTop:(UIView *)view |
644 | { |
645 | CATransition *animation = [CATransition animation]; |
646 | [animation setDuration:0.35f]; |
647 | [animation setFillMode:kCAFillModeForwards]; |
648 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
649 | [animation setType:@ "oglFlip" ]; |
650 | [animation setSubtype:@ "fromTop" ]; |
651 | |
652 | [view.layer addAnimation:animation forKey:nil]; |
653 | } |
654 | |
655 | + ( void )animationFlipFromBottom:(UIView *)view |
656 | { |
657 | CATransition *animation = [CATransition animation]; |
658 | [animation setDuration:0.35f]; |
659 | [animation setFillMode:kCAFillModeForwards]; |
660 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
661 | [animation setType:@ "oglFlip" ]; |
662 | [animation setSubtype:@ "fromBottom" ]; |
663 | |
664 | [view.layer addAnimation:animation forKey:nil]; |
665 | } |
666 | |
667 | + ( void )animationCubeFromLeft:(UIView *)view |
668 | { |
669 | CATransition *animation = [CATransition animation]; |
670 | [animation setDuration:0.35f]; |
671 | [animation setFillMode:kCAFillModeForwards]; |
672 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
673 | [animation setType:@ "cube" ]; |
674 | [animation setSubtype:@ "fromLeft" ]; |
675 | |
676 | [view.layer addAnimation:animation forKey:nil]; |
677 | } |
678 | |
679 | + ( void )animationCubeFromRight:(UIView *)view |
680 | { |
681 | CATransition *animation = [CATransition animation]; |
682 | [animation setDuration:0.35f]; |
683 | [animation setFillMode:kCAFillModeForwards]; |
684 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
685 | [animation setType:@ "cube" ]; |
686 | [animation setSubtype:@ "fromRight" ]; |
687 | |
688 | [view.layer addAnimation:animation forKey:nil]; |
689 | } |
690 | |
691 | + ( void )animationCubeFromTop:(UIView *)view |
692 | { |
693 | CATransition *animation = [CATransition animation]; |
694 | [animation setDuration:0.35f]; |
695 | [animation setFillMode:kCAFillModeForwards]; |
696 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
697 | [animation setType:@ "cube" ]; |
698 | [animation setSubtype:@ "fromTop" ]; |
699 | |
700 | [view.layer addAnimation:animation forKey:nil]; |
701 | } |
702 | |
703 | + ( void )animationCubeFromBottom:(UIView *)view |
704 | { |
705 | CATransition *animation = [CATransition animation]; |
706 | [animation setDuration:0.35f]; |
707 | [animation setFillMode:kCAFillModeForwards]; |
708 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
709 | [animation setType:@ "cube" ]; |
710 | [animation setSubtype:@ "fromBottom" ]; |
711 | |
712 | [view.layer addAnimation:animation forKey:nil]; |
713 | } |
714 | |
715 | + ( void )animationSuckEffect:(UIView *)view |
716 | { |
717 | CATransition *animation = [CATransition animation]; |
718 | [animation setDuration:0.35f]; |
719 | [animation setFillMode:kCAFillModeForwards]; |
720 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
721 | [animation setType:@ "suckEffect" ]; |
722 | |
723 | [view.layer addAnimation:animation forKey:nil]; |
724 | } |
725 | |
726 | + ( void )animationRippleEffect:(UIView *)view |
727 | { |
728 | CATransition *animation = [CATransition animation]; |
729 | [animation setDuration:0.35f]; |
730 | [animation setFillMode:kCAFillModeForwards]; |
731 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
732 | [animation setType:@ "rippleEffect" ]; |
733 | |
734 | [view.layer addAnimation:animation forKey:nil]; |
735 | } |
736 | |
737 | + ( void )animationCameraOpen:(UIView *)view |
738 | { |
739 | CATransition *animation = [CATransition animation]; |
740 | [animation setDuration:0.35f]; |
741 | [animation setFillMode:kCAFillModeForwards]; |
742 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
743 | [animation setType:@ "cameraIrisHollowOpen" ]; |
744 | [animation setSubtype:@ "fromRight" ]; |
745 | |
746 | [view.layer addAnimation:animation forKey:nil]; |
747 | } |
748 | |
749 | + ( void )animationCameraClose:(UIView *)view |
750 | { |
751 | CATransition *animation = [CATransition animation]; |
752 | [animation setDuration:0.35f]; |
753 | [animation setFillMode:kCAFillModeForwards]; |
754 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
755 | [animation setType:@ "cameraIrisHollowClose" ]; |
756 | [animation setSubtype:@ "fromRight" ]; |
757 | |
758 | [view.layer addAnimation:animation forKey:nil]; |
759 | } |
760 | @end |