dismiss ios pop效果_iOS自定义转场动画-present和dismiss
自定义present、dismiss转场动画的步骤和自定义push、pop转场动画的步骤是一致的,相关内容请看《iOS自定义转场动画-push和pop》。
主要涉及的API
push和pop自定义转场动画通过导航控制器的UINavigationControllerDelegate配置,而present和dismiss自定义转场动画通过控制器的本身属性transitioningDelegate(UIViewControllerTransitioningDelegate)配置。
下面是要用到UIViewControllerTransitioningDelegate的几个方法:
//指定present动画
- (nullable id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;
//指定dismiss动画
- (nullable id )animationControllerForDismissedController:(UIViewController *)dismissed;
//指定交互式present动画的控制类
- (nullable id )interactionControllerForPresentation:(id )animator;
//指定交互式dismiss动画的控制类
- (nullable id )interactionControllerForDismissal:(id )animator;
present转场动画
1、准备工作:
ViewController类,要present到的下一级控制器SecondViewController类。
2、实现present动画:
这里演示的动画类似原生的push和pop动画,present时界面由右向左覆盖上一级界面,dismiss相反过程。
HSLeftPresentAnimation类定义:
@interface HSLeftPresentAnimation : NSObject
@property (nonatomic, assign) BOOL isPresent;
@end
为了方便我们将present和dismiss动画写在一起,通过属性isPresent设置动画类型。
@implementation HSLeftPresentAnimation
- (NSTimeInterval)transitionDuration:(id)transitionContext{
return 1.f;
}
- (void)animateTransition:(id)transitionContext{
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* toView = nil;
UIView* fromView = nil;
UIView* transView = nil;
if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
toView = [transitionContext viewForKey:UITransitionContextToViewKey];
} else {
fromView = fromViewController.view;
toView = toViewController.view;
}
if (_isPresent) {
transView = toView;
[[transitionContext containerView] addSubview:toView];
}else {
transView = fromView;
[[transitionContext containerView] insertSubview:toView belowSubview:fromView];
}
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
transView.frame = CGRectMake(_isPresent ?width :0, 0, width, height);
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
transView.frame = CGRectMake(_isPresent ?0 :width, 0, width, height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
@end
3、指定present要使用的转场动画行为:present转场的动画行为是由UIViewControllerTransitioningDelegate协议指定,所以我们设置secondVC的transitioningDelegate属性:
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.transitioningDelegate = self;
[self presentViewController:secondVC animated:YES completion:nil];
实现以下协议,指定动画类:
- (id)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
HSLeftPresentAnimation* leftPresentAnimation = [[HSLeftPresentAnimation alloc] init];
leftPresentAnimation.isPresent = YES;
return leftPresentAnimation;
}
这样present转场动画就完成了,效果图如下:
present转场动画
dimiss转场动画
自定义dimiss转场动画和present动画步骤一样,在SecondViewController类里面指定transitioningDelegate,并实现协议方法即可。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.transitioningDelegate = self;
}
- (id)animationControllerForDismissedController:(UIViewController *)dismissed{
HSLeftPresentAnimation* leftPresentAnimation = [[HSLeftPresentAnimation alloc] init];
leftPresentAnimation.isPresent = NO;
return leftPresentAnimation;
}
dimiss转场动画效果图:
dimiss转场动画效果图
dismiss变为可交互转场动画
现在我们也希望通过向右拖拽界面将界面dismiss掉,上一篇文章有说到可交互转场通过实现UIViewControllerInteractiveTransitioning的协议的类控制,官方为我们提供了一个通过百分比控制交互转场动画的类UIPercentDrivenInteractiveTransition。那么我们就使用它将dismiss变为可交互转场动画。
1、在SecondViewController里面添加手势
//添加手势
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] init];
[pan addTarget:self action:@selector(panGestureRecognizerAction:)];
[self.view addGestureRecognizer:pan];
2、添加手势处理代码:
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)pan{
//产生百分比
CGFloat process = [pan translationInView:self.view].x / ([UIScreen mainScreen].bounds.size.width);
process = MIN(1.0,(MAX(0.0, process)));
if (pan.state == UIGestureRecognizerStateBegan) {
self.interactiveTransition = [UIPercentDrivenInteractiveTransition new];
//触发dismiss转场动画
[self dismissViewControllerAnimated:YES completion:nil];
}else if (pan.state == UIGestureRecognizerStateChanged){
[self.interactiveTransition updateInteractiveTransition:process];
}else if (pan.state == UIGestureRecognizerStateEnded
|| pan.state == UIGestureRecognizerStateCancelled){
if (process > 0.5) {
[ self.interactiveTransition finishInteractiveTransition];
}else{
[ self.interactiveTransition cancelInteractiveTransition];
}
self.interactiveTransition = nil;
}
}
这里和pop手势处理一模一样只是更换了触发dismiss转场动画的代码。
3、通过UIViewControllerTransitioningDelegate指定交互动画的控制类
-(id)interactionControllerForDismissal:(id)animator{
return self.interactiveTransition;
}
现在基本完成了可交互的dismiss转场动画:
可交互的dismiss转场动画
dismiss ios pop效果_iOS自定义转场动画-present和dismiss相关推荐
- dismiss ios pop效果_iOS ~ ViewController的Push,Pop和Present,Dismiss转场动画
转场动画涉及到的包括导航控制器的Push动画和Pop动画,以及普通控制器的Present和Dismiss动画,主要就是通过控制器遵守UIViewControllerTransitioningDeleg ...
- dismiss ios pop效果_iOS 动画框架pop使用方法
pop支持4种动画类型:弹簧动画效果.衰减动画效果.基本动画效果和自定义动画效果. 弹簧动画效果 1.效果图如下: 2.控制器代码如下,首先用pod安装导入pop框架: #import "V ...
- dismiss ios pop效果_iOS实现自定义炫酷的弹出视图(popView)
"前段时间,在项目中有个需求是支付完成后,弹出红包,实现这么一个发红包的功能.做了最后,实现的效果大致如下:" ###一.使用方法 整个ViewController的代码大致如下 ...
- dismiss ios pop效果_iOS实现pop效果(模态一个气泡出来)
前面写过一篇关于UIPopoverPresentationController简单实用的文章,在使用中可能会碰到一个问题,就是点击pop出来的界面之外的地方pop出的界面才会消失,这就导致如果我在po ...
- dismiss ios pop效果_iOS正确解决隐藏导航栏后push和pop或dismiss和present闪黑问题
情景: 一级页面不显示导航栏 ,二级页面显示导航栏. 方法一 适用于push/pop: 一级页面中 - (void)viewWillAppear:(BOOL)animated { [super vie ...
- iOS 自定义转场动画, nav的push/pop自定义动画
本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动画.网易音乐启动屏转场动画.开关门动画.全屏侧滑返回效果 的代码可以到Github WSLTransferA ...
- iOS 自定义转场动画实现小红书的push效果思路以及下雪碎屏等动画的实现
感觉好久没写会动的Demo了,前几天写了很久的Block源码分析,分析了几天整个人都不好了,都不知道block是什么了......,有需要的同学可以去看看,简直不要太简单Block是什么鬼毕竟也是做电 ...
- iOS 自定义转场动画浅谈
代码地址如下: http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差 ...
- iOS 关于自定义转场动画,以UITabBarController为例
1.小记 关于自定义转场动画,只要你理清他的"套路",你就可以随心所欲地自定义了. 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你 ...
- iOS开发 tabbar自定义转场动画
1.小记 关于自定义转场动画,只要你理清他的"套路",你就可以随心所欲地自定义了. 大体思路就是:遵守对应的代理协议,然后设置对应的代理,实现代理方法,这个代理方法要返回的值就是你 ...
最新文章
- 不止于刷榜,三大CV赛事夺冠算法技术的“研”与“用”
- 对校招生培养工作的建议_高校学生会组织深化改革评估工作组对我校学生会复核验收...
- Spring的事务管理1
- 为恶意文件“画像” 瀚思科技基于深度学习技术快速锁定未知威胁
- Scala中使用两种方式对单词进行次数统计(wordCount)
- jh锂电保护电路_锂电池过充电、过放电、过流及短路保护电路原理及电路图
- Android之android studio如何把项目分享到github并提修改的代码到Github
- GC调优在Spark应用中的实践
- CocoaLumberjack
- POJ NOI MATH-7826 分苹果
- linux下运行exe程序之wine的使用与安装
- 快的打车创始人带着区块链“杀”回来了,要与滴滴再次决一死战?
- IP地址-子网掩码-默认网关之间的关系
- WLC RTU license
- 书架html5,基于HTML5 Canvas的CSG构造实体几何书架
- 微型计算机字,小型微型计算机系统
- 神策数据如何帮助企业实现营销自动化?
- c语言编写虚拟光驱软件下载,虚拟光驱(LZZ Virtual Drive)
- Linux内存是怎么工作的?
- 用递归实现求n!阶层和菲波那切数列