博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
push从左到右的动画效果
阅读量:5954 次
发布时间:2019-06-19

本文共 3541 字,大约阅读时间需要 11 分钟。

成功版的从左到右的Push动画

通过实现UINavigationControllerDelegate,自定义转场动画 1.创建一个Pushtransition

#import 
@interface PushTransition : NSObject@property(nonatomic,strong) id
transitionContext;@end复制代码

.m文件

#import 
#import
#import "PushTransition.h"@interface PushTransition ()
@end@implementation PushTransition- (NSTimeInterval)transitionDuration:(nullable id
)transitionContext{ return 0.3f;}// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.- (void)animateTransition:(id
)transitionContext{ self.transitionContext = transitionContext; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; //不添加的话,屏幕什么都没有 UIView *containerView = [transitionContext containerView]; [containerView addSubview:fromVC.view]; [containerView addSubview:toVC.view]; CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"]; maskAnimation.fromValue = @(-toVC.view.frame.size.width/2.0); maskAnimation.toValue = @(toVC.view.frame.size.width/2.0); maskAnimation.duration = [self transitionDuration:transitionContext]; maskAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; maskAnimation.fillMode = kCAFillModeForwards; maskAnimation.removedOnCompletion = NO; maskAnimation.delegate = self; [toVC.view.layer addAnimation:maskAnimation forKey:@"position"];}- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]]; //去除mask [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil; [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;}@end复制代码

2.设置navcontroller 代理

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor redColor];    self.navigationController.delegate = self;}复制代码

3.在viewcontroller实现代理

#pragma mark -UINavigationControllerDelegate- (id
)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{ if (operation == UINavigationControllerOperationPush) { //返回我们自定义的效果 return (id)[[PushTransition alloc]init]; } return nil;}复制代码

4.push跳转

- (IBAction)pushTransition:(id)sender {    SecondViewController *secondCtl = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];    [self.navigationController pushViewController:secondCtl animated:YES];}复制代码

4.FAQ 此处有坑请小心,transition中的animationDidStop必须要有,否则你会发现所有的事件都响应不了.

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];    //去除mask    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;}复制代码

转载地址:http://edoxx.baihongyu.com/

你可能感兴趣的文章
Url.Action取消字符转义
查看>>
JQuery选择器大全
查看>>
Gamma阶段第三次scrum meeting
查看>>
python3之装饰器修复技术@wraps
查看>>
[考试]20150606
查看>>
Javascript_备忘录5
查看>>
Can’t create handler inside thread that has not called Looper.prepare()
查看>>
敏捷开发方法综述
查看>>
Hadoop数据操作系统YARN全解析
查看>>
修改数据库的兼容级别
查看>>
Windows下同时安装两个版本Jdk
查看>>
uoj#228. 基础数据结构练习题(线段树)
查看>>
JS键盘事件监听
查看>>
ios开发周期之--(向上,向下,四舍五入)取整
查看>>
加油!
查看>>
拦截导弹问题(动态规划)
查看>>
iOS 单元测试(Unit Test 和 UI Test)
查看>>
[linux小技巧]
查看>>
文件下载_中文乱码:"Content-disposition","attachment; filename=中文名
查看>>
HBase 笔记3
查看>>