不使用UINavigationController实现Push动画
2023-02-18 15:28:52 时间
在iOS开发中,如果使用UINavigationController,配合Storyboard+Push模式的Segue,默认情况下,可以直接实现左右推出的View切换效果。
但是,如果不使用UINavigationController时,把Segue设置为Push,运行就会直接报错,而Model模式的Segue只有Cover Vertical,Flip Horizontal,Cross Dissolve和Partial Curl这4种模式,没有Push模式。
如果要在自定义的View切换动画中使用Push模式,唯一的方法是把Segue设置为Custom,然后自定义Push动画。
自定义View切换动画效果的实现方式是从UIStoryboardSegue派生一个类,然后在-(void)perform
方法中自己实现动画。
经过多次Google,找到多种实现方式,经比较后发现,最简单最靠谱的Push动画实现如下:
- (void)perform
{
UIViewController* source = (UIViewController *)self.sourceViewController;
UIViewController* destination = (UIViewController *)self.destinationViewController;
CGRect sourceFrame = source.view.frame;
sourceFrame.origin.x = -sourceFrame.size.width;
CGRect destFrame = destination.view.frame;
destFrame.origin.x = destination.view.frame.size.width;
destination.view.frame = destFrame;
destFrame.origin.x = 0;
[source.view.superview addSubview:destination.view];
[UIView animateWithDuration:.25
animations:^{
source.view.frame = sourceFrame;
destination.view.frame = destFrame;
}
completion:^(BOOL finished) {
[source presentViewController:destination animated:NO completion:nil];
}];
}
以上就是从右向左切换View动画的实现,关键代码是:
sourceFrame.origin.x = -sourceFrame.size.width;
和
destFrame.origin.x = destination.view.frame.size.width;
将旧的View向左移动,新的View从最右开始进入。
最后,在动画结束后,手动将当前View切换到新的View:
[source presentViewController:destination animated:NO completion:nil];
因为动画效果我们已经自己实现了,所以切换到新的View就不要使用动画了。
简单修改上面的代码,就可以实现从左到右的Push切换动画:
CGRect sourceFrame = source.view.frame;
sourceFrame.origin.x = sourceFrame.size.width;
CGRect destFrame = destination.view.frame;
destFrame.origin.x = -destination.view.frame.size.width;
destination.view.frame = destFrame;
利用Custom模式的Segue,可以实现任意切换动画效果,对于简单的非3D动画,简单的几行代码即可实现。
相关文章
- 面向接口编程的好处-介绍API数据接口的好处!
- C语言大学生考试全面题库
- 数据治理很抽象吗
- Elasticsearch互联网主流分布式全文检索框架实战-ElasticStack(上)v7.14.0
- 分布式全局ID生成器原理剖析及非常齐全开源方案应用示例
- ElasticJob分布式任务调度应用v2.5.2
- 云原生概念你了解多少
- 主流微服务一站式解决方案Spring Cloud Alibaba入门看这篇就足够了-开篇v2.2.1.RELEASE
- Pulsar云原生分布式消息和流平台v2.8.0
- 回顾2022,展望2023,一个普通98年程序员的自述和分享
- SpringBoot+Mybatis-plus整合easyExcel批量导入Excel到数据库+导出Excel
- Springboot整合策略模式概念->使用场景->优缺点->企业级实战
- 三台服务器使用docker搭建redis一主二从三哨兵,概念-搭建-整合springboot
- IDEA中给源码添加自己注释——private-notes插件安装使用
- docker搭建Elasticsearch、Kibana、Logstash 同步mysql数据到ES
- Spark简单介绍,Windows下安装Scala+Hadoop+Spark运行环境,集成到IDEA中
- SpringBoot自定义注解+异步+观察者模式实现业务日志保存
- 还在使用@Autowrired注入?不妨试试@RequiredArgsConstructor
- 后端统一处理返回前端日期LocalDateTime格式化去T,Long返回前端损失精度问题
- 手写自定义springboot-starter,感受框架的魅力和原理