OC转场动画UIViewControllerTransitioningDelegate

该项目一共两个界面,第一个的只有一个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的更多相关文章
- 教你实现类似于格瓦拉启动页中的放大转场动画(OC&Swift)
教你实现类似于格瓦拉启动页中的放大转场动画(OC&Swift) 一.前言 用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下 在iOS中,在 ...
- 自己定义转场动画--Swift3.0版本号
转场动画这事.说简单也简单.能够通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...
- iOS自定义转场动画实战讲解
iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...
- Swift开发小技巧--自定义转场动画
自定义转场动画 个人理解为重写了被弹出控制器的modal样式,根据自己的样式来显示modal出来的控制器 例:presentViewController(aVC, animated: true, co ...
- iOS 开发--转场动画
"用过格瓦拉电影,或者其他app可能都知道,一种点击按钮用放大效果实现转场的动画现在很流行,效果大致如下:" 本文主讲SWIFT版,OC版在后面会留下Demo下载 在iOS中,在同 ...
- 类似nike+、香蕉打卡的转场动画效果-b
首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcat 的https://onevcat.com/2013/10/vc-tran ...
- iOS 转场动画探究(一)
什么是转场动画: 转场动画说的直接点就是你常见的界面跳转的时候看到的动画效果,我们比较常见的就是控制器之间的Push和Pop,还有Present和Dismiss的时候设置一下系统给我们的modalTr ...
- iOS 转场动画探究(二)
这篇文章是接着第一篇写的,要是有同行刚看到的话建议从前面第一篇看,这是第一篇的地址:iOS 转场动画探究(一) 接着上一篇写的内容: 上一篇iOS 转场动画探究(一)我们说到了转场要素的第四点,把那个 ...
- iOS转场动画封装
写在前面 iOS在modal 或push等操作时有默认的转场动画,但有时候我们又需要特定的转场动画效果,从iOS7开始,苹果就提供了自定义转场的API,模态推送present和dismiss.导航控制 ...
随机推荐
- MySQL主从介绍 准备工作 配置主 配置从 测试主从同步
配置主: • 安装mysql • 修改my.cnf,增加server-id=130和log_bin=xiaobo1 • 添加环境变量 Vim /root/.bash_profile PATH=$PAT ...
- xml 转map dom4j
http://ziyu-1.iteye.com/blog/469003 传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml. 转换规则: 1.主要是Map与 ...
- [Python] 00 - Books
A.I. & Optimization Advanced Machine Learning, Data Mining, and Online Advertising Services Ref: ...
- PHP_CodeSniffer HG 服务端部署篇
环境:CentOs 6.7 语言:PHP5.4 PHP_CodeSniffer: https://github.com/phpdragon/PHP_CodeSniffer 本地代码检测请查看该文章:h ...
- Nginx-配置一个简单的http虚拟服务
配置文件内容如下: #user nobody; worker_processes 4; #工作进程的个数,可以配置多个,一般配置成CPU的核数 pid logs/nginx.pid; # 此文件用于记 ...
- [转]cocos2d-js 3.0 屏幕适配方案 分辨率适应
首先介绍一个api和相应的参数: cc.view.setDesignResolutionSize(1024, 768, cc.ResolutionPolicy.FIXED_WIDTH); 这里设置游戏 ...
- ES6 的模块系统
原文地址:https://hacks.mozilla.org/2015/08/es6-in-depth-modules/ ES6 是 ECMAScript 第 6 版本的简称,这是新一代的 JavaS ...
- <a>标签文字强制不换行
强制不换行 a{ white-space:nowrap; } 再补充说明所有关于换行的CSS样式: white-space: normal|pre|nowrap|pre-wrap|pre-line|i ...
- Android编译系统入门(二)
Android.mk的使用方法 在上一篇Android编译系统入门(一)中我们只要介绍了Android系统使用make命令默认编译的依赖树是droid,而droid是一个伪目标,它有两个先决条件dro ...
- bootstrapValidator多字段联合验证(如开始日期和结束日期中,开始日期不可晚于结束日期)
接触bootstrapvalidator时间不久,最近需要多个字段共同验证,网上查了一下未找到,查阅api文档,发现确实可以实现. 先看dom <div class="form-gro ...