iOS_SN_push/pop转场动画封装和一般动画封装

封装类中的方法:

 1 #import <Foundation/Foundation.h>
 2 
 3 #import <UIKit/UIKit.h>
 4 
 5  
 6 
 7  
 8 
 9 @interface AnimationEffect : NSObject
10 
11  
12 
13  
14 
15 /**
16 
17  *  push/pop转场动画封装
18 
19  *
20 
21  *  @param type           动画类型
22 
23  *  @param subType        动画子类型
24 
25  *  @param duration       动画时间
26 
27  *  @param timingFunction 动画定时函数属性
28 
29  *  @param theView        self.view  当前控制器视图
30 
31  *
32 
33  *  @return 返回一个动画
34 
35  */
36 
37  
38 
39 + (CATransition *)showAnimationType:(NSString *)type
40 
41                         withSubType:(NSString *)subType
42 
43                            duration:(CFTimeInterval)duration
44 
45                      timingFunction:(NSString *)timingFunction
46 
47                                view:(UIView *)theView;
48 
49  
50 
51  
52 
53  
54 
55 @end

封装方法的实现及参数说明:

  1 #import "AnimationEffect.h"
  2 
  3 @implementation AnimationEffect
  4 
  5 
  6 + (CATransition *)showAnimationType:(NSString *)type
  7                         withSubType:(NSString *)subType
  8                            duration:(CFTimeInterval)duration
  9                      timingFunction:(NSString *)timingFunction
 10                                view:(UIView *)theView
 11 {
 12     
 13     CATransition *animation = [CATransition animation];
 14     /** delegate
 15      *
 16      *  动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.
 17      *
 18      *  @see CAAnimationDelegate    (按下command键点击)
 19      */
 20     animation.delegate = self;
 21     /** duration
 22      *
 23      *  动画持续时间
 24      */
 25     animation.duration = duration;
 26     /** timingFunction
 27      *
 28      *  用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是
 29      *  先快后慢,先慢后快还是先慢再快再慢.
 30      *
 31      *  动画的开始与结束的快慢,有五个预置分别为(下同):
 32      *  kCAMediaTimingFunctionLinear            线性,即匀速
 33      *  kCAMediaTimingFunctionEaseIn            先慢后快
 34      *  kCAMediaTimingFunctionEaseOut           先快后慢
 35      *  kCAMediaTimingFunctionEaseInEaseOut     先慢后快再慢
 36      *  kCAMediaTimingFunctionDefault           实际效果是动画中间比较快.
 37      */
 38     
 39     /** timingFunction
 40      *
 41      *  当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
 42      *  具体参见下面的URL
 43      *
 44      *  @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html
 45      *
 46      *  + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
 47      *
 48      *  - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
 49      */
 50     animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
 51     /** fillMode 此设置也可以作为参数传进来
 52      *
 53      *  决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.
 54      *  预置为:
 55      *  kCAFillModeRemoved   默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
 56      *  kCAFillModeForwards  当动画结束后,layer会一直保持着动画最后的状态
 57      *  kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL
 58      *  kCAFillModeBoth      kCAFillModeForwards和kCAFillModeBackwards在一起的效果
 59      */
 60     animation.fillMode = kCAFillModeForwards;
 61     /** type
 62      *
 63      *  各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).
 64      *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
 65      *  @"cube"                     立方体翻滚效果
 66      *  @"moveIn"                   新视图移到旧视图上面
 67      *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
 68      *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)
 69      *  @"pageCurl"                 向上翻一页
 70      *  @"pageUnCurl"               向下翻一页
 71      *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
 72      *  @"rippleEffect"             滴水效果,(不支持过渡方向)
 73      *  @"oglFlip"                  上下左右翻转效果
 74      *  @"rotate"                   旋转效果
 75      *  @"push"
 76      *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
 77      *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
 78      */
 79     
 80     /** type
 81      *
 82      *  kCATransitionFade            交叉淡化过渡
 83      *  kCATransitionMoveIn          新视图移到旧视图上面
 84      *  kCATransitionPush            新视图把旧视图推出去
 85      *  kCATransitionReveal          将旧视图移开,显示下面的新视图
 86      */
 87     animation.type = type;
 88     /** subtype
 89      *
 90      *  各种动画方向
 91      *
 92      *  kCATransitionFromRight;      同字面意思(下同)
 93      *  kCATransitionFromLeft;
 94      *  kCATransitionFromTop;
 95      *  kCATransitionFromBottom;
 96      */
 97     
 98     /** subtype
 99      *
100      *  当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:
101      *  90cw    逆时针旋转90°
102      *  90ccw   顺时针旋转90°
103      *  180cw   逆时针旋转180°
104      *  180ccw  顺时针旋转180°
105      */
106     animation.subtype = subType;
107     [theView.layer addAnimation:animation forKey:nil];
108     return animation;
109 }
110 
111 
112 
113 
114 @end

使用过程push和View:

 1 #import "ViewController.h"
 2 #import "AnimationEffect.h"
 3 #import "TestViewController.h"
 4 
 5 @interface ViewController ()
 6 
 7 @property (nonatomic, strong)UIView *views;
 8 
 9 @end
10 
11 @implementation ViewController
12 
13 - (void)viewDidLoad {
14     [super viewDidLoad];
15 
16 
17     UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
18     bu.frame = CGRectMake(100, 100, 120, 100);
19     [bu setBackgroundColor:[UIColor redColor]];
20     [self.view addSubview:bu];
21     [bu addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
22 
23     
24     self.views = [[UIView alloc]initWithFrame:CGRectMake(100, 230, 120, 200)];
25     self.views.backgroundColor = [UIColor orangeColor];
26     [self.view addSubview:self.views];
27 
28 }
29 - (void)push{
30 //push的使用
31     
32     
33 //    [self.navigationController.view.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
34 //                                                                              withSubType:kCATransitionFromRight
35 //                                                                                 duration:0.5f
36 //                                                                           timingFunction:kCAMediaTimingFunctionEaseInEaseOut
37 //                                                                                     view:self.view]
38 //                                                forKey:@"push"];
39 //    TestViewController *tVC = [[TestViewController alloc]init];
40 //    [self.navigationController pushViewController:tVC animated:YES];
41 
42     
43 //    view的使用
44     [self.views.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
45                                                          withSubType:kCATransitionFromRight
46                                                             duration:0.5f
47                                                       timingFunction:kCAMediaTimingFunctionEaseInEaseOut
48                                                                 view:self.views]
49                             forKey:@"animation"];
50 }

pop的使用过程:

 1 #import "TestViewController.h"
 2 #import "AnimationEffect.h"
 3 
 4 @implementation TestViewController
 5 
 6 
 7 - (void)viewDidLoad {
 8     [super viewDidLoad];
 9     
10     self.view.backgroundColor = [UIColor whiteColor];
11     UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
12     bu.frame = CGRectMake(100, 100, 120, 100);
13     [bu setBackgroundColor:[UIColor purpleColor]];
14     [self.view addSubview:bu];
15     [bu addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
16 }
17 
18 - (void)pop{
19     
20 //    pop的使用
21     [self.navigationController.view.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
22                                                                               withSubType:kCATransitionFromLeft
23                                                                                  duration:0.5f
24                                                                            timingFunction:kCAMediaTimingFunctionEaseInEaseOut
25                                                                                      view:self.view]
26                                                 forKey:@"push"];
27 
28     [self.navigationController popViewControllerAnimated:YES];
29 }
30 
31 @end

 后续将完善modal动画的封装。

本文GitHub地址https://github.com/zhangkiwi/iOS_SN_Animation

原文地址:https://www.cnblogs.com/zhang-kiwi/p/5225252.html