该项目一共两个界面,第一个的只有一个SystemAnimationViewController只有UICollecitonView,第二个界面ImgDetailViewController只有一个UIImageView,代码简单,这里不做陈述

下面是转场动画的所有代码:

//
// ModelAnimationDelegate.m
// Demo
//
// Created by Apple on 2018/11/8.
// Copyright © 2018年 cqytjr. All rights reserved.
// #import "ModelAnimationDelegate.h"
#import "SystemAnimationViewController.h"
#import "SystemAnimaitonCollectionViewCell.h"
#import "ImgDetailViewController.h"
/*
1.描述ViewController转场的:
UIViewControllerTransitioningDelegate自定义模态转场动画时使用
,UINavigationControllerDelegate 自定义navigation转场动画时使用。,
UITabBarControllerDelegate 自定义tab转场动画时使用。
2.定义动画内容的
UIViewControllerAnimatedTransitioning,UIViewControllerInteractiveTransitioning
3.表示动画上下文的
UIViewControllerContextTransitioning
*/
@interface ModelAnimationDelegate ()< UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate>
@property (nonatomic, assign) BOOL isPresentAnimationing;
@end @implementation ModelAnimationDelegate #pragma mark UIViewControllerAnimatedTransitioning // 视图弹出
- (void)presentViewAnimation:(id <UIViewControllerContextTransitioning>)transitionContext { // 获取容器view
UIView *containerView = [transitionContext containerView];
// 获取目标view
UIView *destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
destinationView.alpha = 0;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; // 获取目标vc
ImgDetailViewController *destinationVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
NSIndexPath *indexPath = destinationVC.indexPath; // 获取来源vc
UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SystemAnimationViewController *sourceVC = (SystemAnimationViewController *)naVC.topViewController;
// 获取来源view
UICollectionView *collectionView = sourceVC.collectionView;
SystemAnimaitonCollectionViewCell *selectedCell = (SystemAnimaitonCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
// 获取动画开始位置大小
CGRect startFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.img.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFit;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView]; // 获取动画结束位置大小
CGRect endFrame = destinationVC.img.frame; // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame;
NSLog(@"-----:%f %f %f %f",startFrame.origin.x,startFrame.origin.y,startFrame.size.width,startFrame.size.height);
NSLog(@"-----:%f %f %f %f",endFrame.origin.x,endFrame.origin.y,endFrame.size.width,endFrame.size.height); } completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
[UIView animateWithDuration:1.0 animations:^{
destinationView.alpha = 1.0;
} completion:^(BOOL finished) {
[animationImgView removeFromSuperview];
}];
}]; } // 视图消失
- (void)dismissViewAnimation:(id <UIViewControllerContextTransitioning>)transitionContext { // 获取容器view
UIView *containerView = [transitionContext containerView];
// 获取目标view
UIView *destinationView = [transitionContext viewForKey:UITransitionContextToViewKey];
destinationView.alpha = 0;
// 将目标view添加到容器view
[containerView addSubview:destinationView]; UINavigationController *naVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
// 获取目标vc
SystemAnimationViewController *destinationVC = naVC.topViewController;
// NSIndexPath *indexPath = destinationVC.indexPath;
//
// 获取来源vc ImgDetailViewController *sourceVC = (ImgDetailViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
NSIndexPath *indexPath = sourceVC.indexPath;
// 获取来源view
UICollectionView *collectionView = destinationVC.collectionView;
SystemAnimaitonCollectionViewCell *selectedCell = (SystemAnimaitonCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; // 获取动画开始位置大小
CGRect startFrame = sourceVC.img.frame; UIImageView *animationImgView = [[UIImageView alloc] initWithFrame:startFrame];
animationImgView.image = selectedCell.img.image;
animationImgView.contentMode = UIViewContentModeScaleAspectFill;
animationImgView.clipsToBounds = YES;
[containerView addSubview:animationImgView];
animationImgView.contentMode = UIViewContentModeScaleAspectFit;
animationImgView.clipsToBounds = YES; // 获取动画结束位置大小
CGRect endFrame = [collectionView convertRect:selectedCell.frame toView:[UIApplication sharedApplication].keyWindow]; // 执行过渡动画
[UIView animateWithDuration:1.0 animations:^{
animationImgView.frame = endFrame; NSLog(@"-----:%f %f %f %f",startFrame.origin.x,startFrame.origin.y,startFrame.size.width,startFrame.size.height);
NSLog(@"-----:%f %f %f %f",endFrame.origin.x,endFrame.origin.y,endFrame.size.width,endFrame.size.height);
sourceVC.view.hidden = YES; } completion:^(BOOL finished) {
[transitionContext completeTransition:YES]; }];
[UIView animateWithDuration:1.0 animations:^{
destinationView.alpha = 1.0; } completion:^(BOOL finished) {
[animationImgView removeFromSuperview];
}];
} - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
return 10.0;
} - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
_isPresentAnimationing ? [self presentViewAnimation:transitionContext] : [self dismissViewAnimation:transitionContext]; } #pragma mark UIViewControllerTransitioningDelegate - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
_isPresentAnimationing = YES;
return self;
} - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
_isPresentAnimationing = NO;
return self; } @end

OC转场动画UIViewControllerTransitioningDelegate的更多相关文章

  1. 教你实现类似于格瓦拉启动页中的放大转场动画(OC&Swift)

    教你实现类似于格瓦拉启动页中的放大转场动画(OC&Swift) 一.前言 用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下 在iOS中,在 ...

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

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

  3. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

  4. Swift开发小技巧--自定义转场动画

    自定义转场动画 个人理解为重写了被弹出控制器的modal样式,根据自己的样式来显示modal出来的控制器 例:presentViewController(aVC, animated: true, co ...

  5. iOS 开发--转场动画

    "用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同 ...

  6. 类似nike+、香蕉打卡的转场动画效果-b

    首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcat 的https://onevcat.com/2013/10/vc-tran ...

  7. iOS 转场动画探究(一)

    什么是转场动画: 转场动画说的直接点就是你常见的界面跳转的时候看到的动画效果,我们比较常见的就是控制器之间的Push和Pop,还有Present和Dismiss的时候设置一下系统给我们的modalTr ...

  8. iOS 转场动画探究(二)

    这篇文章是接着第一篇写的,要是有同行刚看到的话建议从前面第一篇看,这是第一篇的地址:iOS 转场动画探究(一) 接着上一篇写的内容: 上一篇iOS 转场动画探究(一)我们说到了转场要素的第四点,把那个 ...

  9. iOS转场动画封装

    写在前面 iOS在modal 或push等操作时有默认的转场动画,但有时候我们又需要特定的转场动画效果,从iOS7开始,苹果就提供了自定义转场的API,模态推送present和dismiss.导航控制 ...

随机推荐

  1. LostRoutes项目日志——敌人精灵Enemy解析

    Enemy类在Enemy.js中,类Enemy类继承自PhysicsSprite,以便于可以使用物理引擎中的一些特性. 原版的Enemy.js: var Enemy = cc.PhysicsSprit ...

  2. Maven & Gradle 如何从中央仓库下载Jar包

    https://mvnrepository.com/    maven 中央仓库 gradle可以轻松的完成Android项目对第三方jar包文件的依赖下载,再也不需要我们手动下载jar包,然后拷贝到 ...

  3. Kafka创建Topic时如何将分区放置到不同的Broker中

    熟悉 Kafka的同学肯定知道,每个主题有多个分区,每个分区会存在多个副本,本文今天要讨论的是这些副本是怎么样放置在 Kafka集群的 Broker 中的. 大家可能在网上看过这方面的知识,网上对这方 ...

  4. python分支语句

    一.if else语句 if 条件表达式: else: a = 3 b = 4 if a >= b: print("a >= b") else: print(" ...

  5. sublime3 配置go的开发环境

    配置go的环境变量:https://studygolang.com/articles/4910 配置sublime:https://studygolang.com/articles/4938

  6. 火狐浏览器报错“support.mozilla.org

    火狐浏览器有时候再打开新网页会报此错“support.mozilla.org 有时候火狐浏览器会出现如下状况 解决方法 在地址栏键入”about:config” 点击“我了解此风险” 在下方任意位置右 ...

  7. KMP 算法详解

    之前模模糊糊的理解了KMP,结果由于并不是完全弄清楚而导致自己在一道题目上疯狂的T,似乎是next函数写的有问题,于是痛心疾首的回来写一篇报告,警示自己 对KMP来说,匹配串的next数组是重中之重, ...

  8. day11 十一、函数对象,名称空间,作用域,和闭包

    一.函数对象 1.函数对象:函数名存放的就是函数的地址,所以函数名也是对象,称之为函数对象 a = 10 print(a,id(a)) def fn(): num = 10 print('fn fuc ...

  9. 二叉苹果树|codevs5565|luoguP2015|树形DP|Elena

    二叉苹果树 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的 ...

  10. 在Ubuntu环境下安装eclipse

    Eclipse运行需要Java环境,java环境的安装见https://www.cnblogs.com/Sabre/p/10349320.html,本文不再赘述. 1.下载eclipse eclips ...