自己定义modal动画
在非常多场景中。我们都须要实现各种动画。这回我们来尝试搞一下控制器间跳转的modal动画。
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ZYSecondViewController *second = [[ZYSecondViewController alloc]init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:second animated:YES completion:nil];
}
上面是系统提供的动画的样式,可是系统提供的动画有时候满足不了我们的需求,所以我们就要自己定义动画了。我们接下来。就重点的来说一下自己定义动画这一块的内容。
准备工作:我之前写过的单例:一行代码搞定单例
以及一些坐标的扩展,这里我们直接调用一下:
ZYtransition.h(单例)
#import "Singleton.h"
@interface ZYtransition : NSObject <UIViewControllerTransitioningDelegate>
SingletonH(transition)
@end
- ZYtransition.m
#import "ZYtransition.h"
#import "ZYAnimatedTransitioning.h"
#import "ZYPresentationController.h"
@implementation ZYtransition
SingletonM(transition)
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
ZYPresentationController *pc = [[ZYPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
return pc;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
anim.presented = YES;
return anim;
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
anim.presented = NO;
return anim;
}
- ZYAnimatedTransitioning.h (负责动画)
@interface ZYAnimatedTransitioning : NSObject<UIViewControllerAnimatedTransitioning>
@property(nonatomic,assign)BOOL presented;
@end
- ZYAnimatedTransitioning.m
const NSTimeInterval duraton = 0.5;
@implementation ZYAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
return duraton;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
// UITransitionContextToViewKey
// UITransitionContextFromViewKey
if (self.presented)
{
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.y = -toView.height;
[UIView animateWithDuration:duraton animations:^{
toView.y = 0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}else
{
UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
[UIView animateWithDuration:duraton animations:^{
fromView.y = -fromView.height;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
@end
- 搞一个ZYPresentationController
.h
@interface ZYPresentationController : UIPresentationController
.m
@implementation ZYPresentationController
- (void)presentationTransitionWillBegin
{
self.presentedView.frame = self.containerView.bounds;
[self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{
// NSLog(@"%s",__func__);
}
- (void)dismissalTransitionWillBegin
{
// NSLog(@"%s",__func__);
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
[self.presentedView removeFromSuperview];
}
@end
这种话,我们外面用起来就非常easy了:
#import "ViewController.h"
#import "ZYSecondViewController.h"
#import "ZYtransition.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ZYSecondViewController *second = [[ZYSecondViewController alloc]init];
second.modalPresentationStyle = UIModalPresentationCustom;
second.transitioningDelegate = [ZYtransition sharedtransition];
[self presentViewController:second animated:YES completion:nil];
}
@end
外面仅仅须要跟平时一样。然后设置显示的样式为自己定义,然后制定代理。就能实现modal的自己定义动画效果了。
自己定义modal动画的更多相关文章
- android动画介绍之 自己定义Animation动画实现qq抖一抖效果
昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...
- iOS_20_微博自己定义可动画切换的导航控制器
终于效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/ ...
- Android使用xml中定义的动画效果
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...
- 如何用CSS定义一个动画?
<style type="text/css"> div{ width:100px;height: 100px; animation: carton 5s; backgr ...
- 自己定义View Controller转换动画
原文链接 : Introduction to Custom View Controller Transitions and Animations 原文作者 : joyce echessa 译文出自 : ...
- css3 动画效果 定义和绑定执行
首先要定义一个动画效果 keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式 有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写 (这里是一般的 ...
- 自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,"掏粪男孩Gif"顺便再提提onWindowFocusChanged)
转载请注明出处:王亟亟的大牛之路 之前在绘画的过程中提到了静态的旋转啊,缩放啊,平移等一些效果.那么自己定义的View当然也有动态的效果也就是我们的Animation.经常使用的有三种 View An ...
- 自己定义转场动画--Swift3.0版本号
转场动画这事.说简单也简单.能够通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...
- iOS 自己定义页面的切换动画与交互动画 By Swift
在iOS7之前,开发人员为了寻求自己定义Navigation Controller的Push/Pop动画,仅仅能受限于子类化一个UINavigationController,或是用自己定义的动画去覆盖 ...
随机推荐
- sqlserver中case when then用法
sql语句判断方法之一,Case具有两种格式,简单Case函数和Case搜索函数. --简单Case函数 (CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' E ...
- python gui tkinter用法杂记
1.treeview遍历 iids = tree.selection() t = tree.get_children() for i in t: print(tree.item(i,'values') ...
- 厦门大学XMUNET+ ubuntu连接方法
编辑连接 输入用户名和密码就好了 ubuntu16.04
- CentOS6.9下安装 Pika 2.2.5(新增了拷贝安装版本的办法+对于PID的位置及数据库位置的理解)
一.环境准备 yum install -y snappy-devel protobuf-compiler protobuf-devel bzip2-devel zlib-devel bzip2 ...
- Django 静态文件配置 (Nginx)
初学Django,在访问静态文件时候遇到很多误区,一直配置不成功,在此记录一下. Django静态文件访问分为两种:一种是Debug模式下,测试开发网站时对静态文件的访问,一种是实际生产环境中对静态文 ...
- Mysql优化之——启用查询缓存
启用MySQL查询缓存可以极大地减低数据库服务器的CPU使用率,实际使用情况是:开启前CPU使用率120%左右,开启后降到了10%. 查看查询缓存情况: mysql> show variable ...
- QT_QMAKE_EXECUTABLE reported QT_INSTALL_LIBS as /usr/lib/i386-linux-gnu but ...
$sudo apt-get install libqt4-dev done!!!
- MongoDB走过的坑(4.0.3版本)
数据存储一般使用本地或者存储在数据库,MongoDB是一个非关系型数据库,今天小结下走过的一些坑. 1.网上的很多教程对自己无效 解决方法:这种情况一般都是和版本有关系,数据库在不断的更新发展,很多东 ...
- Codeforces 1059E. Split the Tree
题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...
- 数学结论【p1463】[POI2002][HAOI2007]反素数
Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...