当前位置: 首页 > 编程日记 > 正文

iOS 自定义转场动画初探

最近项目刚迭代,正好闲下来捣鼓了一下iOS的自定义转场的效果。闲话不多说,直接开始上代码吧。(ps:请忽略实际的转场效果,关注技术本身呢哦。pps:主要是转场的动画做的比较low啦!)

1、首先定义一个转场动画的类

第一步,咱们先定义一个管理转场动画的类。首先创建一个继承自NSObject的类,这里叫JTAnimatedTransition,并且让其遵循UIViewControllerAnimatedTransitioning这个协议。
      这个是.h文件代码。 我在这里定义了一个枚举,用来表示转场的类型。并且还遵循的CAAnimationDelegate协议,这个后面会说到。 再有呢,就是一个类方法了,这个是在使用的控制器中会用到的方法,具体的用法后续代码中会有体现。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef enum {JTAnimatedTransitionTypePush,JTAnimatedTransitionTypePop,JTAnimatedTransitionTypePresent,JTAnimatedTransitionTypeDismiss
}JTAnimatedTransitionType;@interface JTAnimatedTransition : NSObject<UIViewControllerAnimatedTransitioning, CAAnimationDelegate>
@property (assign, nonatomic) JTAnimatedTransitionType type;
@property (strong, nonatomic) id transitionContext;
+ (JTAnimatedTransition *)animatedTransitionWithType:(JTAnimatedTransitionType)type;
@end

接下来是.m中的代码了。 具体说明在代码中都有详细注释。

#import "JTAnimatedTransition.h"
@implementation JTAnimatedTransition+ (JTAnimatedTransition *)animatedTransitionWithType:(JTAnimatedTransitionType)type
{JTAnimatedTransition *animatedTransition = [[JTAnimatedTransition alloc] init];animatedTransition.type = type;return animatedTransition;
}#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{//返回动画执行的时间return 0.5;
}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{//在这里设置自己想要的动画效果self.transitionContext = transitionContext;if (self.type == JTAnimatedTransitionTypePush) {// 获得即将消失的vc的vUIView *fromeView = [transitionContext viewForKey:UITransitionContextFromViewKey];// 获得即将出现的vc的vUIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];// 获得容器viewUIView *containerView = [transitionContext containerView];[containerView addSubview:fromeView];[containerView addSubview:toView];UIBezierPath *startBP = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((containerView.frame.size.width-100)/2, 100, 100, 100)];CGFloat radius = 1000;UIBezierPath *finalBP = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(150 - radius, 150 -radius, radius*2, radius*2)];CAShapeLayer *maskLayer = [CAShapeLayer layer];maskLayer.path = finalBP.CGPath;toView.layer.mask = maskLayer;//执行动画CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];animation.delegate = self;animation.fromValue = (__bridge id _Nullable)(startBP.CGPath);animation.toValue = (__bridge id _Nullable)(finalBP.CGPath);animation.duration = [self transitionDuration:transitionContext];animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];[maskLayer addAnimation:animation forKey:@"path"];} else if (self.type == JTAnimatedTransitionTypePresent) {UIView *fromeView = [transitionContext viewForKey:UITransitionContextFromViewKey];UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];UIView *containerView = [transitionContext containerView];[containerView addSubview:fromeView];[containerView addSubview:toView];fromeView.frame = containerView.frame;toView.frame = containerView.frame;toView.layer.anchorPoint = CGPointMake(0.0f, 1.0f);toView.frame = containerView.frame;toView.transform = CGAffineTransformMakeRotation(-M_PI/2);[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{toView.transform = CGAffineTransformIdentity;} completion:^(BOOL finished) {[self.transitionContext completeTransition:YES];}];} else if (self.type == JTAnimatedTransitionTypeDismiss) {UIView *fromeView = [transitionContext viewForKey:UITransitionContextFromViewKey];UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];UIView *containerView = [transitionContext containerView];[containerView addSubview:fromeView];[containerView addSubview:toView];fromeView.frame = containerView.frame;toView.frame = CGRectMake(containerView.frame.size.width, containerView.frame.origin.y, 0, containerView.frame.size.height);[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{fromeView.frame = CGRectMake(containerView.frame.origin.x, containerView.frame.origin.y, 0, containerView.frame.size.height);toView.frame = containerView.frame;} completion:^(BOOL finished) {[self.transitionContext completeTransition:YES];}];} else {}
}#pragma mark - CAAnimationDelegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{//告诉系统转场动画完成[self.transitionContext completeTransition:YES];//清除相应控制器视图的mask[self.transitionContext viewForKey:UITransitionContextFromViewKey].layer.mask = nil;[self.transitionContext viewForKey:UITransitionContextToViewKey].layer.mask = nil;
}@end

到这里转场动画管理者就写好了,后面我们只需要关注使用者的情况了。

2、怎么使用转场动画

现在我们创建一个使用者类。我这里就直接使用了ViewController了。下面是创建好之后的效果图。


首页.png

下面就是.m的具体实现代码了,一些简单的界面搭建代码就不贴出来了。下面的代码只是一些跟转场动画有关的代码哦。

这是点击cell之后的逻辑
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{[tableView deselectRowAtIndexPath:indexPath animated:YES];ViewControllerTwo *vc = [[ViewControllerTwo alloc]init];UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];cell.imageView.hidden = YES;self.selectImageView.image = cell.imageView.image;self.selectImageView.frame = CGRectMake(cell.imageView.frame.origin.x, rect.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height);vc.img = self.selectImageView.image;vc.row = indexPath.row;[UIView animateWithDuration:0.5 animations:^{self.selectImageView.frame = CGRectMake(0, 2, self.view.bounds.size.width/2, self.view.bounds.size.width/2);self.selectImageView.center = CGPointMake(self.view.bounds.size.width/2, 200);} completion:^(BOOL finished) {[self.navigationController pushViewController:vc animated:YES];}];
}

要实现自定义的转场,还需要使用者必须遵循UINavigationControllerDelegate协议或者, UIViewControllerTransitioningDelegate协议。根据需要的专场类型,实现自己的协议方法。

#pragma mark - pop / push
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{if (operation == UINavigationControllerOperationPush) {JTAnimatedTransition *animatedTransition = [JTAnimatedTransition animatedTransitionWithType:JTAnimatedTransitionTypePush];return animatedTransition;}return nil;
}- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{if (viewController != self) {self.selectImageView.frame = CGRectNull;}
}#pragma mark - present / dismiss
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{return [JTAnimatedTransition animatedTransitionWithType:JTAnimatedTransitionTypePresent];
}- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{return [JTAnimatedTransition animatedTransitionWithType:JTAnimatedTransitionTypeDismiss];
}

首页push效果图.gif

3、模态跳转


模态跳转效果图.gif

调用跳转的代码

-(void)click:(UIButton *)sender
{ViewControllerThree *vc = [[ViewControllerThree alloc]init];[self presentViewController:vc animated:YES completion:^{}];
}调用者 ViewController 内实现了协议方法

在返回时 在 ViewControllerThree 内需要遵循 UIViewControllerTransitioningDelegate 并实现其协议方法

#import "ViewControllerThree.h"
#import "JTAnimatedTransition.h"
@interface ViewControllerThree ()<UIViewControllerTransitioningDelegate>@end@implementation ViewControllerThree- (void)viewDidLoad {[super viewDidLoad];self.transitioningDelegate = self;self.title = @"模态";self.view.backgroundColor = [UIColor lightGrayColor];UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];btn.backgroundColor = [UIColor redColor];[btn setTitle:@"点我返回" forState:UIControlStateNormal];[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];
}
-(void)back
{[self dismissViewControllerAnimated:YES completion:^{}];
}#pragma mark - UIViewControllerTransitioningDelegate- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{return [JTAnimatedTransition animatedTransitionWithType:JTAnimatedTransitionTypePresent];
}- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{return [JTAnimatedTransition animatedTransitionWithType:JTAnimatedTransitionTypeDismiss];
}@end

结束语

写到这转场动画就自定义完成了。其实转场动画主要就是两个协议:1、在动画管理类内遵循UIViewControllerAnimatedTransitioning 2、在动画使用的类内遵循, UIViewControllerTransitioningDelegate以及UINavigationControllerDelegate协议,并实现其协议方法。
      如果你有什么更加炫酷的动画,只需要在JTAnimatedTransition内设置动画的位置写上自己想要的动画即可。


相关文章:

Delphi实现WebService带身份认证的数据传输

WebService使得不同开发工具开发出来的程序可以在网络连通的环境下相互通信,它最大的特点就是标准化(基于XML的一系列标准)带来的跨平台、跨开发工具的通用性,基于HTTP带来的畅通无阻的能力(跨越防火墙)。WebService给我们的软件开发带来了诸多好处,但是有一点还是必须要考虑到…

【Linux学习笔记】 - 什么是Linux?

Linux Linux内核 GNU工具 组成部分 Linux内核GUN工具图形化桌面环境应用软件 Linux内核 地位&#xff1a;Linux核心&#xff0c;控制计算机系统上的所有硬件和软件。必要时&#xff0c;分配硬件&#xff0c;并根据需要执行软件 主要功能&#xff1a; a. 系统内存存储 ——…

【转】 Android快速开发系列 10个常用工具类 -- 不错

原文网址&#xff1a;http://blog.csdn.net/lmj623565791/article/details/38965311 转载请标明出处&#xff1a;http://blog.csdn.net/lmj623565791/article/details/38965311&#xff0c;本文出自【张鸿洋的博客】 打开大家手上的项目&#xff0c;基本都会有一大批的辅助类&a…

CollectionView侧滑刷新

作者 SoDoIt 关注 2017.03.05 16:39 字数 33 阅读 31评论 0喜欢 2ABSideRefresh.gif效仿MJRefresh写的侧滑刷新&#xff0c;原理不讲了&#xff0c;需要的直接看代码 GitHub&#xff1a;https://github.com/wangjingyu0018/ABRefresh.git

函数功能MATLAB

近期一直在查找函数功能之类的题问,现在正好有机会和大家享共一下. 百科名片 录目 简介开展程历要主功能新特性版本分析特色优势开展简介开展程历要主功能新特性版本分析特色优势开展编辑本段 简介 matlab开辟任务面界 编辑本段 开展程历 编辑本段 要主功能 1.数值析分 2.数值和…

[HTTP协议]基础篇-待完结

文章目录输入网址后回车输入网址后回车 简单的浏览器HTTP请求过程&#xff1a; 浏览器从地址栏输入中获取服务器IP地址和端口号浏览器用TCP的三次握手与服务器建立连接浏览器向服务器发送拼好的报文服务器收到报文后处理请求&#xff0c;同样拼好报文再发给浏览器浏览器解析报…

IAR之工程配置

参考 : IAR的Workspace顶部下拉菜单中Debug和Release http://blog.csdn.net/yanpingsz/article/details/5588525 最近买了zigbee模块的开发板回来研究, 其中一个实验程序里面有三个版本, 分别是路由/终端/协调器, 忙活了半天不知道同一个project是如何配置成3个不同的版本的. …

CoreText入坑一

CoreText是Mac OS和iOS系统中处理文本的low-level API, 不管是使用OC还是swift, 实际我们使用CoreText都还是间接或直接使用C语言在写代码。CoreText是iOS和Mac OS中文本处理的根基, TextKit和WebKit都是构建于其上。 一. 基础 1.在使用CoreText编写代码之前, 需要先了解一些基…

mysql连接hang住问题分析

【问题现象】&#xff1a; 1. Linuxc多线程连接mysql数据库&#xff0c;每次都是短连接&#xff0c;操作完后就释放连接&#xff0c;有时候会出现mysql_real_connect挂住的现象 2. 挂住超时mysql_real_connect返回后报错如下&#xff1a;Lostconnection to MySQL s…

【Linux学习笔记】 -- 基本Shell命令

常见的目录名均基于文件系统层级标准(filesystem hierarchy standard&#xff0c;FHS) Linux的四个部分&#xff1a; 1 Linux内核&#xff1a;控制所有硬软件&#xff0c;必要时分配硬件根据需要执行软件 系统内存管理&#xff1a;可用物理内存 创建、管理虚拟内存[交换空间…

【OpenCV】图像代数运算:平均值去噪,减去背景

代数运算&#xff0c;就是对两幅图像的点之间进行加、减、乘、除的运算。四种运算相应的公式为&#xff1a; 代数运算中比较常用的是图像相加和相减。图像相加常用来求平均值去除addtive噪声或者实现二次曝光&#xff08;double-exposure&#xff09;。图像相减用于减去背景或周…

简明 Vim 练级攻略(转)

vim的学习曲线相当的大&#xff08;参看各种文本编辑器的学习曲线&#xff09;&#xff0c;所以&#xff0c;如果你一开始看到的是一大堆VIM的命令分类&#xff0c;你一定会对这个编辑器失去兴趣的。下面的文章翻译自《Learn Vim Progressively》&#xff0c;我觉得这是给新手最…

iOS 的离屏渲染

原文链接&#xff1a;http://www.imlifengfeng.com/blog/?p593OpenGL ES 是一套多功能开放标准的用于嵌入系统的 C-based 的图形库&#xff0c;用于 2D 和 3D 数据的可视化。OpenGL 被设计用来转换一组图形调用功能到底层图形硬件&#xff08;GPU&#xff09;&#xff0c;由 G…

MySQL 常见操作指令

什么是SQL&#xff1f; SQL&#xff08;Structured Query Language&#xff09;用于访问和操作数据库的结构化查询语言。 数据库包含一个或多个表&#xff0c;每个表均有名称标识&#xff0c;包含数据的记录&#xff08;行&#xff09;。 典型的SQL语句 1. SELEC语句 SELE…

iOS 实现点击微信头像效果

来源&#xff1a;伯乐在线 - 小良 如有好文章投稿&#xff0c;请点击 → 这里了解详情 如需转载&#xff0c;发送「转载」二字查看说明 公司产品需要实现点击个人主页头像可以放大头像、缩放头像、保存头像效果&#xff08;和点击微信个人头像类似&#xff09;&#xff0c;故找…

HDU 4292 Food(dinic +拆点)

题目链接 我做的伤心了&#xff0c;不知是模版效率低&#xff0c;还是错了&#xff0c;交上就是TLE&#xff0c;找了份别人的代码&#xff0c;改了好几下终于过了。。 1 #include <cstdio>2 #include <cstring>3 #include <queue>4 #include <map>5 #i…

jQuery中用ajax访问php接口文件

js代码 function ajax_request(){var result;var articleId new Object();articleIdgetArticleId();$.ajax({url: "/topicPage/getComment.php",//请求php文件的路径data:{id:articleId},//请求中要传送的参数,会自动拼接成一个路径&#xff0c;在php中用get方式获取…

Python 数据库操作 psycopg2

文章目录安装基本使用安装 psycopg 是 Python 语言中 PostpreSQL数据库接口 安装环境&#xff1a; Python&#xff1a;v2.7, v3.4~3.8PostGreSQL&#xff1a;7.4~12 pip install psycopg2基本使用 import psycopg2def connect_db(host: str,port: int,database: str,user:…

Android logcat命令详解

一、logcat命令介绍 1.android log系统 2.logcat介绍 logcat是android中的一个命令行工具&#xff0c;可以用于得到程序的log信息 log类是一个日志类&#xff0c;可以在代码中使用logcat打印出消息 常见的日志纪录方法包括&#xff1a;方法 描述 v(String,String) (vervbose)显…

[iOS]如何重新架构 JPVideoPlayer ?

注意&#xff1a;此文为配合 JPVideoPlayer version 2.0 版本发布而写&#xff0c;如果你想了解 2.0 版本的更新内容和所有实现细节&#xff0c;请点击前往 GitHub。 导言&#xff1a;我几个月前写了一个在 UITableView 中滑动 UITableViewCell 播放视频的框架&#xff0c;类似…

函数项目一个超感人的故事:关于swfupload在某些环境下面session丢失的完美解决方案(看完我哭了)...

查了好多资料&#xff0c;发现还是不全&#xff0c;干脆自己整理吧&#xff0c;至少保证在我的做法正确的&#xff0c;以免误导读者&#xff0c;也是给自己做个记录吧&#xff01; 标题吸引到你了吗&#xff1f; 先说一下这个题问成形的原因。大家都晓得 session是靠cookie中的…

【学习笔记】git 使用文档

安装 git # mac 环境 brew install git检查是否安装成功 ➜ ~ git --version git version 2.20.1 (Apple Git-117)卸载 git ➜ ~ which -a git /usr/bin/git ➜ ~ cd /usr/bin ➜ bin sudo rm -rf git*git init 命令 对一个空文件&#xff0c;git 初始化。文件名称增加…

UIBezierPath和CAShapeLayer创建不规则View(Swift 3.0)

最近一个朋友在做图片处理的 App&#xff0c;想要实现类似 MOLDIV App 拼图的UI效果&#xff08;如何创建不规则的 view&#xff09;&#xff0c;就问我有什么想法。我首先想到的就是 UIBezierPathCAShapeLayer的方式&#xff0c;为了验证自己的想法&#xff0c;写了一个小 dem…

http响应状态

Servlet API&#xff1a; javax.servlet.http.HttpServletResponse 用于创建HTTP响应&#xff0c;包括HTTP协议的状态行、响应头以及消息体 HTTP状态码&#xff1a; 100-199&#xff1a;表示信息性代码&#xff0c;标示客户端应该采取的其他动作&#xff0c;请求正在进行。 200…

antlr.collections.AST.getLine()I问题的起因及解决

在我们的java web 项目中引入hibernate和struts&#xff0c;当我们使用HQL语句进行查询时会报 antlr.collections.AST.getLine()I的错误&#xff0c;导致程序无法继续运行&#xff0c;这并不是我们的程序写的有错误&#xff0c;出现这个异常的原因是因为我们使用的hibernate和s…

2018湖湘杯海选复赛Writeup

2018湖湘杯Writeup0x01 签到题0x02 MISC Flow0x03 WEB Code Check0x04 WEB Readflag0x05 WEB XmeO0x06 Reverse Replace0x07 MISC Disk0x08 Crypto Common Crypto0x09 Reverse HighwayHash640x10 Web Mynot0x01 签到题 关注合天智汇公众号&#xff0c;回复hxb2018得到flag。0x…

Operation Queues并发编程

并发、异步在我们的编程中&#xff0c;见到的太多了。在iOS中&#xff0c;实现并发的主要三个途径Operation Queues、Dispatch Queues、Dispatch Sources&#xff0c;今天我们就来详细介绍Operatin Queues的使用&#xff0c;花了两天时间写这一篇&#xff0c;值得一看。 为什么…

socket 服务器浏览器与服务器客户端实例

一、服务器与浏览器 // 取得本机的loopback网络地址&#xff0c;即127.0.0.1 IPAddress address IPAddress.Loopback; IPEndPoint endPoint new IPEndPoint(address, 49152); Socket socket new Socket(AddressFamily.InterNetwork, Socke…

匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,也可以不用,区号与本地号间可以用连字号或空格间隔,也可以没有间隔...

public bool IsPhone(string input){string pattern "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";Regex regex new Regex(pattern);return regex.IsMatch(input);} 转载于:https://www.cnblogs.com/…

Mac MySQL配置环境变量的两种方法

第一种&#xff1a; 1.打开终端,输入&#xff1a; cd ~ 会进入~文件夹 2.然后输入&#xff1a;touch .bash_profile 回车执行后&#xff0c; 3.再输入&#xff1a;open -e .bash_profile 会在TextEdit中打开这个文件&#xff08;如果以前没有配置过环境变量&#xff0c;那么这…