Transition 过渡/转场动画(一)
UIViewController 的转场效果
当viewController通过push 或 present 进行转场时, 系统自带的动画是从右侧push进来一个新的viewControler (或从下面present 一个新的ViewController), 接下来我们要做的就是要自定义系统的这个动画效果.
原理: 比如当viewController 调用 dismiss 后, 系统会检查当前 vc 是否实现<UIViewControllerTransitioningDelegate> 协议, 该协议会返回 自定义 的转场动画
例: 通过pan手势, dismiss 当前viewController
1. 在调用dimiss的 VC 中实现 <UIViewControllerTransitioningDelegate> 协议
#import "SecondViewController.h"
#import "CustomInteractiveTransition.h"
#import "CustomDissmissAnimation.h" @interface SecondViewController () <UIViewControllerTransitioningDelegate> @property (nonatomic, strong) CustomInteractiveTransition *interactiveAnimator;
@property (nonatomic, strong) CustomDissmissAnimation *dismissAnimator; @end @implementation SecondViewController - (instancetype)init {
if (self = [super init]) {
self.transitioningDelegate = self;
}
return self;
} - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"dismiss" forState:UIControlStateNormal];
button.frame = CGRectMake(, , , );
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; self.interactiveAnimator = [[CustomInteractiveTransition alloc] initWithViewController:self]; self.dismissAnimator = [[CustomDissmissAnimation alloc] init]; UIPanGestureRecognizer *panGestureRecognizer =[[UIPanGestureRecognizer alloc] initWithTarget:self.interactiveAnimator action:@selector(panGestureAction:)];
[self.view addGestureRecognizer:panGestureRecognizer]; [self.transitionCoordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { }];
} - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated]; [self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
UIView *view = [context viewForKey:UITransitionContextFromViewKey];
view.transform = CGAffineTransformMakeScale(0.7, 0.7); } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
// UIView *view = [context viewForKey:UITransitionContextFromViewKey];
// view.transform = CGAffineTransformIdentity;
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; [self.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
UIView *view = [context viewForKey:UITransitionContextToViewKey];
view.transform = CGAffineTransformMakeScale(, ); } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
// UIView *view = [context viewForKey:UITransitionContextToViewKey];
// view.transform = CGAffineTransformIdentity;
}];
} - (void)dealloc
{
self.interactiveAnimator = nil;
self.dismissAnimator = nil;
} - (void)btnClick:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
} #pragma mark - UIViewControllerTransitioningDelegate
// return 动画对象,该动画对象符合 UIViewControllerAnimatedTransitioning 协议,负责显示 present 动画。
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
return nil;
}
// return 动画对象,负责显示 dismiss 动画。
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
return self.dismissAnimator;
}
// return 交互式动画对象,该动画符合 UIViewControllerInteractiveTransitioning 协议,采用触摸手势或手势识别器作为动画的驱动,显示 present 动画。
- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id<UIViewControllerAnimatedTransitioning>)animator {
return nil;
}
// return 交互式动画,显示 dismiss 动画
- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
// 如果直接返回 interactive 会与系统的 dismiss 动画有冲突,导致点击 button 无法 dismiss 界面。
// 同时如果返回 interactiveAnimator,那么 animationControllerForDismissedController: 则必须实现
return self.interactiveAnimator.isInteractive? self.interactiveAnimator: nil;
// return self.interactiveAnimator;
} // return UIPresentationController,系统已经提供了各个演示样式。
//- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0); @end
2. 实现一个动画对象, 实现 <UIViewControllerAnimatedTransitioning>协议
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
return 0.35f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *srcVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *secondVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *containerView = [transitionContext containerView];
[containerView addSubview:srcVC.view];
[containerView addSubview:secondVC.view];
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
secondVC.view.frame = CGRectMake(, [UIScreen mainScreen].bounds.size.height, secondVC.view.bounds.size.width, secondVC.view.bounds.size.height);
} completion:^(BOOL finished) {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
}];
}
3. 交互式动画效果, 需要实现<UIViewControllerInteractiveTransitioning>协议 ; 或者继承UIPercentDrivenInteractiveTransition 类
* 可选实现. 在本例中, 通过pan手势下滑跟随dismiss, 需要通过交互式动画来实现
#import "CustomInteractiveTransition.h"
@interface CustomInteractiveTransition : UIPercentDrivenInteractiveTransition @property (nonatomic, assign, readonly) BOOL isInteractive; - (instancetype)initWithViewController:(UIViewController *)viewController; - (void)panGestureAction:(UIPanGestureRecognizer *)gestureRecognizer; @end
//.m
@interface CustomInteractiveTransition () @property (nonatomic, weak) UIViewController *viewController; @property (nonatomic, assign) CGFloat startScale; @property (nonatomic, assign, readwrite) BOOL isInteractive; @end @implementation CustomInteractiveTransition - (instancetype)initWithViewController:(UIViewController *)viewController {
if (self = [super init]) {
_isInteractive = NO;
_viewController = viewController;
}
return self;
} - (void)panGestureAction:(UIPanGestureRecognizer *)recognizer {
CGFloat progress = [recognizer translationInView:self.viewController.view].y / (self.viewController.view.bounds.size.height * 1.0);
progress = MIN(1.0, MAX(0.0, progress)); self.isInteractive = YES; if (recognizer.state == UIGestureRecognizerStateBegan) { [self.viewController dismissViewControllerAnimated:YES completion:^{ }];
}
else if (recognizer.state == UIGestureRecognizerStateChanged) {
[self updateInteractiveTransition:progress];
}
else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
if (progress > 0.5) {
[self finishInteractiveTransition];
}
else {
[self cancelInteractiveTransition];
} self.isInteractive = NO;
}
} @end
在 iOS中,可以取消一个过渡。这意味着,第二个视图的-viewWillApear被调用,但-viewDidApear不一定被调用。
如果代码写的假定-viewDidAppear总是在-viewWillAppear之后执行则需要重新考虑逻辑实现。
这种情况下UIViewControllerTransitionCoordinator就有用了。在交互式过渡结束的时候,会在 block 中收到通知。
参考:
present: https://www.jianshu.com/p/aed8a3a15c82
push/pop: https://www.jianshu.com/p/28b9523d70a9
Transition 过渡/转场动画(一)的更多相关文章
- 【CSS3】transition过渡和animation动画
转自:http://blog.csdn.net/XIAOZHUXMEN/article/details/52003135 写在前面的话: 最近写css动画发现把tansition和animation弄 ...
- 自己总结的CSS3中transform变换、transition过渡、animation动画的基本用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- iOS 动画学习之视图控制器转场动画
一.概述 1.系统会创建一个转场相关的上下文对象,传递到动画执行器的animateTransition:和transitionDuration:方法,同样,也会传递到交互Controller的star ...
- 转场动画CALayer (Transition)
1.将对应UI控件的层调用以下接口即可 1.1 .h文件 // // 文 件 名:CALayer+Transition.h // // 版权所有:Copyright © 2018年 leLight. ...
- 基于 React 实现一个 Transition 过渡动画组件
过渡动画使 UI 更富有表现力并且易于使用.如何使用 React 快速的实现一个 Transition 过渡动画组件? 基本实现 实现一个基础的 CSS 过渡动画组件,通过切换 CSS 样式实现简单的 ...
- iOS:核心动画之转场动画CATransition
转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 U ...
- iOS 转场动画探究(一)
什么是转场动画: 转场动画说的直接点就是你常见的界面跳转的时候看到的动画效果,我们比较常见的就是控制器之间的Push和Pop,还有Present和Dismiss的时候设置一下系统给我们的modalTr ...
- iOS 转场动画探究(二)
这篇文章是接着第一篇写的,要是有同行刚看到的话建议从前面第一篇看,这是第一篇的地址:iOS 转场动画探究(一) 接着上一篇写的内容: 上一篇iOS 转场动画探究(一)我们说到了转场要素的第四点,把那个 ...
- iOS转场动画封装
写在前面 iOS在modal 或push等操作时有默认的转场动画,但有时候我们又需要特定的转场动画效果,从iOS7开始,苹果就提供了自定义转场的API,模态推送present和dismiss.导航控制 ...
随机推荐
- Netty之揭开BootStrap 的神秘面纱
客户端BootStrap: Bootstrap 是Netty 提供的一个便利的工厂类, 我们可以通过它来完成Netty 的客户端或服务器端的Netty 初始化.下面我先来看一个例子, 从客户端和服务器 ...
- CSU 1092 Barricade
1092: Barricade Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 240 Solved: 71[Submit][Status][Web Bo ...
- Codeforces - 1194E - Count The Rectangles - 扫描线
https://codeforc.es/contest/1194/problem/E 给5000条正常的(同方向不会重叠,也不会退化成点的)线段,他们都是平行坐标轴方向的,求能组成多少个矩形. 先进行 ...
- iBatis——自动生成DAO层接口提供操作函数(详解)
iBatis——自动生成DAO层接口提供操作函数(详解) 在使用iBatis进行持久层管理时,发现在使用DAO层的updateByPrimaryKey.updateByPrimaryKeySelect ...
- NGUI的anchors属性的使用
一,anchors锚点 我们需要明白target目标的使用,这时是你下面使用left,right,bottom和top的距离,比如我们使用目标为UI Root,这个就是摄像机的视野,所以,我们使用an ...
- k3 cloud查看附件提示授予目录NetWorkService读写权限
打开文件的时候出现下面的提示: 解决办法: 解决办法:找到C:\Program Files(x86)\Kingdee\K3Cloud\WebSite\FileUpLoadServices,在下面创建F ...
- 解决chrome浏览器安装不上的问题
1. 打开注册表: windows键 + R --> 输入regedit --> 回车 (注:windows键在左ctrl附近微软图标的键) 2. 找到 32位:HKEY_LOCA ...
- Dubbo源码学习总结系列一 总体认识
本文写作时,dubbo最高版本是V2.6.0. 写这篇文章主要想回答以下4个问题: 一.dubbo是什么?完成了哪些主要需求? 二.dubbo适用于什么场景? 三.dubbo的总体架构是什么样的? ...
- shell截取小数点前后的子串
- vue,一路走来(9)--聊天窗口
闲暇时间,介绍一下我做一个聊天窗口的心得.如图: 首先要考虑的是得判断出是自己的信息还是对方发来的信息,给出如图的布局,切换不同的类. <li class="clearfix" ...