在非常多场景中。我们都须要实现各种动画。这回我们来尝试搞一下控制器间跳转的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动画的更多相关文章

  1. android动画介绍之 自己定义Animation动画实现qq抖一抖效果

    昨天我们介绍了Animation的基本使用方法.小伙伴们了解的怎么样了?假设还没有了解过Animation的小伙伴能够看看这篇博客 android动画介绍--Animation 实现loading动画 ...

  2. iOS_20_微博自己定义可动画切换的导航控制器

    终于效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/ ...

  3. Android使用xml中定义的动画效果

    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.zqrl_out); animation.setFil ...

  4. 如何用CSS定义一个动画?

    <style type="text/css"> div{ width:100px;height: 100px; animation: carton 5s; backgr ...

  5. 自己定义View Controller转换动画

    原文链接 : Introduction to Custom View Controller Transitions and Animations 原文作者 : joyce echessa 译文出自 : ...

  6. css3 动画效果 定义和绑定执行

    首先要定义一个动画效果  keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式  有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写  (这里是一般的 ...

  7. 自己定义View时,用到Paint Canvas的一些温故,简单的帧动画(动画一 ,&quot;掏粪男孩Gif&quot;顺便再提提onWindowFocusChanged)

    转载请注明出处:王亟亟的大牛之路 之前在绘画的过程中提到了静态的旋转啊,缩放啊,平移等一些效果.那么自己定义的View当然也有动态的效果也就是我们的Animation.经常使用的有三种 View An ...

  8. 自己定义转场动画--Swift3.0版本号

    转场动画这事.说简单也简单.能够通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...

  9. iOS 自己定义页面的切换动画与交互动画 By Swift

    在iOS7之前,开发人员为了寻求自己定义Navigation Controller的Push/Pop动画,仅仅能受限于子类化一个UINavigationController,或是用自己定义的动画去覆盖 ...

随机推荐

  1. JS:body元素对象的clientWidth、offsetWidth、scrollWidth、clientLeft、offsetLeft、scrollLeft

    document.body.clientWidth 获取body元素对象的内容可视区域的宽度,即clientWidth=width+padding,不包括滚动条. document.body.clie ...

  2. mybatis-config.xml的解释(zz)

    <!-- xml标准格式 --><?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE ...

  3. 通过docker info命令,可以了解很多信息

    来个输出吧. Containers: 1 Running: 1 Paused: 0 Stopped: 0 Images: 1 Server Version: 17.03.1-ce Storage Dr ...

  4. AC日记——LOOPS hdu 3853

    3853 思路: 概率dp求期望: 代码: #include <cstdio> #include <cstring> #include <iostream> usi ...

  5. JS动态计算移动端rem的解决方案

    首先介绍下rem 说起rem就的说px,em: PX为单位 在Web页面初期制作中,我们都是使用“px”来设置我们的文本,因为他比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的 ...

  6. HDU 变形课 1181【DFS/BFS】

    变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submissi ...

  7. 设计模式-观察者模式(Observer Pattern)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 观察者模式简述 观察者模式的使用非常广泛,常用于建立起一种多对一的关系,该模式一定会包含两个角色 ...

  8. python3中zipfile模块的常用方法

    一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...

  9. cocos2d-x 扩展 修改 备注

    1.引擎源码相关扩展     说明:class/cellsExt 下的全部文件为扩展文件,有auto字样的文件为生成文件,*.pkg文件为自动生成文件的接口配置档,参考tolua++,源文件中代在[s ...

  10. C语言实现括号配对问题

    代码如下: #include<stdio.h> #include<string.h> #include<stdlib.h> // 写一个判断的括号是否匹配的函数 i ...