ios - 自定义UINavigationController push和pop动画
PHP中文网
PHP中文网 2017-04-17 11:15:56
[iOS讨论组]

默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界面切换的动画效果复杂一点,大概有这么几个:

  • 更快的左右推出,默认是0.3秒,我需要快一倍
  • 带抖动的左右推出
  • 水平翻转
  • 纸张翻页效果

但是这些切换都要放在NavigationController里做管理,怎么实现,求个思路

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(4)
黄舟
CATransition* transition = [CATransition animation];
transition.type = kCATransitionPush;//可更改为其他方式
transition.subtype = kCATransitionFromLeft;//可更改为其他方式
transition.duration=0.15;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:viewController animated:YES];
黄舟

1. 如果楼主想要使用UINavigationController中的view controller stack,并且还想要同时自定义push、pop的动画效果,基本上是不可能的。原因在于想要使用view controller stack,就无法躲开
pushViewController:animated:这个方法,而一旦使用pushViewController:animated:,UINavigationController就会强制将要推入的viewController的frame设为当前可视区域的frame,从而断绝一切想要自定义动画的后路,例如如下代码,是不会产生任何效果的:

UINavigationController+CustomAnimation.h

@interface UINavigationController (CustomAnimation)

- (void)customPushViewController:(UIViewController *)viewController;

@end

UINavigationController+CustomAnimation.m

#import "UINavigationController+CustomAnimation.h"

- (void)customPushViewController:(UIViewController *)viewController
{
    viewController.view.frame = (CGRect){0, -viewController.view.frame.size.height, viewController.view.frame.size};
    [self pushViewController:viewController animated:NO];
    [UIView animateWithDuration:.15f
                     animations:^{
                         viewController.view.frame = (CGRect){0, 0, self.view.bounds.size};
                     }];
}

2. 如果仅仅是想添加自定义的push、pop动画,而不使用view controller stack,那么,如下代码就可以实现一个从上到下的push效果:

UINavigationController+CustomAnimation.h文件同上。

UINavigationController+CustomAnimation.m

#import "UINavigationController+CustomAnimation.h"
- (void)customPushViewController:(UIViewController *)viewController
{
    viewController.view.frame = (CGRect){0, -viewController.view.frame.size.height, viewController.view.frame.size};
    [self.topViewController.view addSubview:viewController.view];
    [UIView animateWithDuration:.15f
                     animations:^{
                         viewController.view.frame = (CGRect){0, 0, self.view.bounds.size};
                     }];
}
天蓬老师

虽然基于navctrl,但是你可以不用navctrl push的方法,可以用viewctrl的presentModalViewController。至于速度的话,这些系统的默认都是0.3s,动画方式也就是系统默认的几个。
如果需要各种自定义动画,能自定义动画的是view,给个例子http://www.cocoachina.com/bbs/read.ph...,供楼主参考下。

大家讲道理

参考下http://segmentfault.com/q/10100000001...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号