iOS 导航转场动画
2014-09-19 14:43
686 查看
1.前言:
导航控制器的push,pop的动画默认是推过来,然后返回来,但是ios7后 就可以修改导航的转场动画了,导航,tab 都可以自定义动画 ,昨天心血来潮做了一个demo,研究的不是很深,就给自己做个笔记。
iOS7后有这个协议UIViewControllerAnimatedTransitioning 来修改转场动画 必须实现两个协议方法
// This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
2.准备:
a.两个试图控制器
b.一个动画类别
3.代码
appdelegate
=================================================
动画类
Animator.h
实现 UIViewControllerAnimatedTransitioning协议
/ This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
完整代码:
=================================================
IndexViewController
设置导航的代理
//导航代理
self.navigationController.delegate =self;
实例化动画类
//动画
self.animalPush = [[Animatoralloc]init];
self.animalPush.type =0;
self.animalPop = [[Animatoralloc]init];
self.animalPop.type =1;
然后代理里面实现
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController
*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController
*)toVC
{
if (operation ==UINavigationControllerOperationPush)
{
returnself.animalPush;
}
else
{
returnself.animalPop;
}
return
nil;
}
完整类
=================================================
ViewController
完整类
效果
导航控制器的push,pop的动画默认是推过来,然后返回来,但是ios7后 就可以修改导航的转场动画了,导航,tab 都可以自定义动画 ,昨天心血来潮做了一个demo,研究的不是很深,就给自己做个笔记。
iOS7后有这个协议UIViewControllerAnimatedTransitioning 来修改转场动画 必须实现两个协议方法
// This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
2.准备:
a.两个试图控制器
b.一个动画类别
3.代码
appdelegate
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.window setBackgroundColor:[UIColor whiteColor]]; IndexViewController *vc = [[IndexViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES;
=================================================
动画类
Animator.h
实现 UIViewControllerAnimatedTransitioning协议
/ This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
// This method can only be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
完整代码:
// // Animator.h // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. #import <Foundation/Foundation.h> @interface Animator : NSObject <UIViewControllerAnimatedTransitioning> @property(nonatomic)int type; @property(nonatomic,strong)UIView *view; @end
</pre><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(0, 132, 0);"><pre name="code" class="html">// // Animator.m // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. // #import "Animator.h" @interface Animator() { CGRect rect; } @property(nonatomic,strong)UIView *snapshotView; @end @implementation Animator - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { if (self.type == 0) return 1; else return 1; } -(void)setView:(UIView *)view { if (_view!=view) { _view = view; } rect = view.frame; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; [[transitionContext containerView] addSubview:toViewController.view]; toViewController.view.alpha = 0; //push if (self.type == 0) { [UIView animateWithDuration:[self transitionDuration:transitionContext]*(2.0/3) delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.8 options:UIViewAnimationOptionAllowUserInteraction animations:^{ self.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2); CGAffineTransform trans = CGAffineTransformMakeScale(5,5); self.view.transform = trans; fromViewController.view.transform = CGAffineTransformMakeScale(0.3, 0.3); } completion:^(BOOL finished) { [UIView animateWithDuration:[self transitionDuration:transitionContext]*(1.0/3) animations:^{ fromViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5); toViewController.view.alpha = 1; }completion:^(BOOL finished) { fromViewController.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformIdentity; self.view.frame = rect; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }]; } else//pop { [UIView animateWithDuration:[self transitionDuration:transitionContext]*(1.0/3) animations:^{ fromViewController.view.transform = CGAffineTransformMakeScale(2, 2); } completion:^(BOOL finished) { [UIView animateWithDuration:[self transitionDuration:transitionContext]*(1/3.0) delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.8 options:UIViewAnimationOptionAllowUserInteraction animations:^{ fromViewController.view.transform = CGAffineTransformMakeScale(0, 0); }completion:^(BOOL finished) { toViewController.view.alpha = 1; fromViewController.view.transform = CGAffineTransformIdentity; [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; }]; } } @end
=================================================
IndexViewController
设置导航的代理
//导航代理
self.navigationController.delegate =self;
实例化动画类
//动画
self.animalPush = [[Animatoralloc]init];
self.animalPush.type =0;
self.animalPop = [[Animatoralloc]init];
self.animalPop.type =1;
然后代理里面实现
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController
*)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController
*)toVC
{
if (operation ==UINavigationControllerOperationPush)
{
returnself.animalPush;
}
else
{
returnself.animalPop;
}
return
nil;
}
完整类
// // IndexViewController.h // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. // #import <UIKit/UIKit.h> @interface IndexViewController : UIViewController @end
// // IndexViewController.m // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. // #import "IndexViewController.h" #import "ViewController.h" #import "Animator.h" #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1] //主色调 #define kMAINCOLOR RGBCOLOR(86,206,186) @interface IndexViewController ()<UIViewControllerTransitioningDelegate, UINavigationControllerDelegate, UITabBarControllerDelegate> @property(nonatomic,strong) Animator *animalPush,*animalPop; @end @implementation IndexViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; //动画 self.animalPush = [[Animator alloc] init]; self.animalPush.type = 0; self.animalPop = [[Animator alloc] init]; self.animalPop.type = 1; //导航代理 self.navigationController.delegate = self; float w = [UIScreen mainScreen].bounds.size.width; for (int i = 0; i<6; i++) { UIView *view = [[UIView alloc]initWithFrame:CGRectMake((w/6+5)*i, 100, 50,50)]; [view setBackgroundColor:kMAINCOLOR]; [self.view addSubview:view]; } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = touches.anyObject; UIView *view = [touch view]; self.animalPush.view = view; ViewController *vc = [[ViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { if (operation == UINavigationControllerOperationPush) { return self.animalPush; } else { return self.animalPop; } return nil; } @end
=================================================
ViewController
完整类
// // ViewController.h // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
// // ViewController.m // demo // // Created by linpeng on 14-9-18. // Copyright (c) 2014年 linpeng. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; UILabel *lab = [[UILabel alloc] initWithFrame:self.view.bounds]; lab.text = @"Hello World"; lab.textAlignment = NSTextAlignmentCenter; [self.view addSubview:lab]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.navigationController popToRootViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
效果
相关文章推荐
- iOS中用在导航控制器的转场动画(pop,push动画)制作
- iOS 7:自定义导航转场动画以及更多
- iOS 7:自定义导航转场动画以及更多
- iOS 7:自定义导航转场动画以及更多
- 零基础学习IOS开发(四)- cocos2d-x3.0 使用转场动画和按钮
- 论坛源码推荐(5月22日):快速创建iOS原生图表 视图间的dynamic转场动画
- ios中在uiNavigationcontroller中做转场动画
- ios 实现导航控制器带动画的推送页面
- iOS7使用代理自定义导航转场动画
- iOS7教程系列:自定义导航转场动画以及更多
- CATransition转场动画-IOS开发
- IOS动画(5)转场动画
- CATransition转场动画-IOS开发
- iOS新特性之转场动画
- iOS_20_微博自定义可动画切换的导航控制器
- iOS7—自定义导航转场动画以及更多
- iOS学习阶段总结-b20120920-动画转场
- IOS-转场动画Quartz Core的Core Animation函数包
- ios中uiview 转场动画