iOS中用在导航控制器的转场动画(pop,push动画)制作
2015-11-13 22:14
856 查看
新建了一个CustomTransization动画类继承自
UIPercentDrivenInteractiveTransition
并遵守<UIViewControllerAnimatedTransitioning,UINavigationControllerDelegate>协议
创建动画代理初始化方法并传入导航控制器:
- (instancetype)
initWithNavigationController:(UINavigationController*)nc
{
self = [superinit];
if (self)
{
nc.delegate=
self;
}
return
self;
}
实现动画代理:
#pragma mark - UIViewControllerAnimatedTransitioning protocol
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController
*fromVC = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController
*toVC = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIView
*containerView = [transitionContext
containerView];
containerView.backgroundColor= [UIColorwhiteColor];
[containerView
addSubview:toVC.view];
CGRect
fromFrame = fromVC.view.frame;
CGRect
toFrame = toVC.view.frame;
fromFrame.origin.x=
-fromFrame.size.width;
toFrame.origin.x=
containerView.frame.size.width;
[toVC.viewsetFrame:toFrame];
toFrame.origin.x=
0;
[UIViewanimateWithDuration:[selftransitionDuration:transitionContext]
delay:0
usingSpringWithDamping:0.6f
initialSpringVelocity:15
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[fromVC.viewsetFrame:fromFrame];
[toVC.viewsetFrame:toFrame];
}
completion:^(BOOLfinished)
{
[transitionContext
completeTransition:YES];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return
0.5;
}
#pragma mark - UINavigationControllerDelegate
/**
*
关键实现这俩个方法
UINavigationControllerOperation包含了push还是pop的信息
*/
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController*)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC
{
return
self;
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController*)navigationController
interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
return
nil;
}
在需要的控制器创建动画代理对象
@property (nonatomic,strong)CustomTransization
*transition;
self.transition
= [[CustomTransizationalloc]initWithNavigationController:self.navigationController];
直接执行跳转就可以显示动画了
SecondViewController
*sec = [[SecondViewControlleralloc]init];
[self.navigationControllershowViewController:secsender:sec];
UIPercentDrivenInteractiveTransition
并遵守<UIViewControllerAnimatedTransitioning,UINavigationControllerDelegate>协议
创建动画代理初始化方法并传入导航控制器:
- (instancetype)
initWithNavigationController:(UINavigationController*)nc
{
self = [superinit];
if (self)
{
nc.delegate=
self;
}
return
self;
}
实现动画代理:
#pragma mark - UIViewControllerAnimatedTransitioning protocol
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController
*fromVC = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController
*toVC = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIView
*containerView = [transitionContext
containerView];
containerView.backgroundColor= [UIColorwhiteColor];
[containerView
addSubview:toVC.view];
CGRect
fromFrame = fromVC.view.frame;
CGRect
toFrame = toVC.view.frame;
fromFrame.origin.x=
-fromFrame.size.width;
toFrame.origin.x=
containerView.frame.size.width;
[toVC.viewsetFrame:toFrame];
toFrame.origin.x=
0;
[UIViewanimateWithDuration:[selftransitionDuration:transitionContext]
delay:0
usingSpringWithDamping:0.6f
initialSpringVelocity:15
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[fromVC.viewsetFrame:fromFrame];
[toVC.viewsetFrame:toFrame];
}
completion:^(BOOLfinished)
{
[transitionContext
completeTransition:YES];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return
0.5;
}
#pragma mark - UINavigationControllerDelegate
/**
*
关键实现这俩个方法
UINavigationControllerOperation包含了push还是pop的信息
*/
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController*)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC
{
return
self;
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController*)navigationController
interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
return
nil;
}
在需要的控制器创建动画代理对象
@property (nonatomic,strong)CustomTransization
*transition;
self.transition
= [[CustomTransizationalloc]initWithNavigationController:self.navigationController];
直接执行跳转就可以显示动画了
SecondViewController
*sec = [[SecondViewControlleralloc]init];
[self.navigationControllershowViewController:secsender:sec];
相关文章推荐
- iOS在程序中控制系统的屏幕亮度
- iOS巅峰之背景添加毛玻璃
- #在蓝懿学习iOS的日子#day18
- iOS如何提高tableView的性能
- iOS开发之自定义键盘附件关闭虚拟键盘
- 【译】17个提升iOS开发效率的必用工具
- ios6.1&ios7&ios9判断沙盒文件或者目录是否存在,以及判断是文件还是目录的一个隐藏问题
- ios 使用CFStringTransform将汉字转为拼音
- 关于iOS中的通知中心(NSNotificationCenter)
- 蓝懿ios 技术内容交流和学习心得 11.13
- iOS汉字转拼音(NSString+Characters)为NSString添加的类目
- iOS真机不能运行,模拟器可以
- 总结iOS 多线程学习过程三
- iOS计算缓存文件大小, 和清除缓存功能
- iOS 对plist增改操作
- iOS 基础知识的回顾和梳理
- IOS启动页设置适应ios8/9
- iOS事件处理系列1-事件的种类与处理流程
- iOS开发语言之OC 初级内存管理
- iOS中的自动释放池