封装类中的方法:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface AnimationEffect : NSObject /** * push/pop转场动画封装 * * @param type 动画类型 * @param subType 动画子类型 * @param duration 动画时间 * @param timingFunction 动画定时函数属性 * @param theView self.view 当前控制器视图 * * @return 返回一个动画 */ + (CATransition *)showAnimationType:(NSString *)type withSubType:(NSString *)subType duration:(CFTimeInterval)duration timingFunction:(NSString *)timingFunction view:(UIView *)theView; @end
封装方法的实现及参数说明:
#import "AnimationEffect.h" @implementation AnimationEffect + (CATransition *)showAnimationType:(NSString *)type
withSubType:(NSString *)subType
duration:(CFTimeInterval)duration
timingFunction:(NSString *)timingFunction
view:(UIView *)theView
{ CATransition *animation = [CATransition animation];
/** delegate
*
* 动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法.
*
* @see CAAnimationDelegate (按下command键点击)
*/
animation.delegate = self;
/** duration
*
* 动画持续时间
*/
animation.duration = duration;
/** timingFunction
*
* 用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是
* 先快后慢,先慢后快还是先慢再快再慢.
*
* 动画的开始与结束的快慢,有五个预置分别为(下同):
* kCAMediaTimingFunctionLinear 线性,即匀速
* kCAMediaTimingFunctionEaseIn 先慢后快
* kCAMediaTimingFunctionEaseOut 先快后慢
* kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢
* kCAMediaTimingFunctionDefault 实际效果是动画中间比较快.
*/ /** timingFunction
*
* 当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction
* 具体参见下面的URL
*
* @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html
*
* + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
*
* - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y;
*/
animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
/** fillMode 此设置也可以作为参数传进来
*
* 决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后.
* 预置为:
* kCAFillModeRemoved 默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
* kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
* kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL
* kCAFillModeBoth kCAFillModeForwards和kCAFillModeBackwards在一起的效果
*/
animation.fillMode = kCAFillModeForwards;
/** type
*
* 各种动画效果 其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).
* ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.
* @"cube" 立方体翻滚效果
* @"moveIn" 新视图移到旧视图上面
* @"reveal" 显露效果(将旧视图移开,显示下面的新视图)
* @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果)
* @"pageCurl" 向上翻一页
* @"pageUnCurl" 向下翻一页
* @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
* @"rippleEffect" 滴水效果,(不支持过渡方向)
* @"oglFlip" 上下左右翻转效果
* @"rotate" 旋转效果
* @"push"
* @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向)
* @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向)
*/ /** type
*
* kCATransitionFade 交叉淡化过渡
* kCATransitionMoveIn 新视图移到旧视图上面
* kCATransitionPush 新视图把旧视图推出去
* kCATransitionReveal 将旧视图移开,显示下面的新视图
*/
animation.type = type;
/** subtype
*
* 各种动画方向
*
* kCATransitionFromRight; 同字面意思(下同)
* kCATransitionFromLeft;
* kCATransitionFromTop;
* kCATransitionFromBottom;
*/ /** subtype
*
* 当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为:
* 90cw 逆时针旋转90°
* 90ccw 顺时针旋转90°
* 180cw 逆时针旋转180°
* 180ccw 顺时针旋转180°
*/
animation.subtype = subType;
[theView.layer addAnimation:animation forKey:nil];
return animation;
} @end
使用过程push和View:
#import "ViewController.h"
#import "AnimationEffect.h"
#import "TestViewController.h" @interface ViewController () @property (nonatomic, strong)UIView *views; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
bu.frame = CGRectMake(, , , );
[bu setBackgroundColor:[UIColor redColor]];
[self.view addSubview:bu];
[bu addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside]; self.views = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
self.views.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.views]; }
- (void)push{
//push的使用 // [self.navigationController.view.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
// withSubType:kCATransitionFromRight
// duration:0.5f
// timingFunction:kCAMediaTimingFunctionEaseInEaseOut
// view:self.view]
// forKey:@"push"];
// TestViewController *tVC = [[TestViewController alloc]init];
// [self.navigationController pushViewController:tVC animated:YES]; // view的使用
[self.views.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
withSubType:kCATransitionFromRight
duration:0.5f
timingFunction:kCAMediaTimingFunctionEaseInEaseOut
view:self.views]
forKey:@"animation"];
}
pop的使用过程:
#import "TestViewController.h"
#import "AnimationEffect.h" @implementation TestViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor];
UIButton *bu = [UIButton buttonWithType:UIButtonTypeCustom];
bu.frame = CGRectMake(, , , );
[bu setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:bu];
[bu addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
} - (void)pop{ // pop的使用
[self.navigationController.view.layer addAnimation:[AnimationEffect showAnimationType:@"cube"
withSubType:kCATransitionFromLeft
duration:0.5f
timingFunction:kCAMediaTimingFunctionEaseInEaseOut
view:self.view]
forKey:@"push"]; [self.navigationController popViewControllerAnimated:YES];
} @end
后续将完善modal动画的封装。