类似nike+、香蕉打卡的转场动画效果-b
首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcat 的https://onevcat.com/2013/10/vc-transition-in-ios7/ 对于转场动画的讲解和实现,非常详细,本人也是看过他们的文章后受的启发,快速实现了基于本项目需求的转场动画效果。
效果如下:(gif是我们的美术大师帮忙做的演示动效,实际的效果要比这个好,可通过文章最后链接下载我们的app:柠檬跑步,查看效果)
地图切换.gif
我的需求是两个页面push、pop之间的切换,所以自定义了push的转场动画,首先需要个遵循<UIViewControllerAnimatedTransitioning>协议的管理对象,并实现其两个方法:
XWCircleSpreadTransition.h
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, XWCircleSpreadTransitionType) {
XWCircleSpreadTransitionTypePush = 0,
XWCircleSpreadTransitionTypePop
};
@interface XWCircleSpreadTransition : NSObject<UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) XWCircleSpreadTransitionType type;
+ (instancetype)transitionWithTransitionType:(XWCircleSpreadTransitionType)type;
- (instancetype)initWithTransitionType:(XWCircleSpreadTransitionType)type;@end
XWCircleSpreadTransition.m
#import "XWCircleSpreadTransition.h"
@implementation XWCircleSpreadTransition
+ (instancetype)transitionWithTransitionType:(XWCircleSpreadTransitionType)type{
return [[self alloc] initWithTransitionType:type];
}
- (instancetype)initWithTransitionType:(XWCircleSpreadTransitionType)type{
self = [super init];
if (self) {
_type = type;
}
return self;
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 0.7;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
switch (_type) {
case XWCircleSpreadTransitionTypePush:
[self presentAnimation:transitionContext];
break;
case XWCircleSpreadTransitionTypePop:
[self dismissAnimation:transitionContext];
break;
}
}
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView *containerView = [transitionContext containerView];
[containerView insertSubview:toVC.view atIndex:0]; //画两个圆路径
CGFloat radius = sqrtf(containerView.frame.size.height * containerView.frame.size.height + containerView.frame.size.width * containerView.frame.size.width) / 2; UIBezierPath *startCycle = [UIBezierPath bezierPathWithArcCenter:containerView.center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];// UIBezierPath *endCycle = [UIBezierPath bezierPathWithOvalInRect:temp.mapBtnFrame];
UIBezierPath *endCycle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 0, 0)]; //创建CAShapeLayer进行遮盖
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillColor = [UIColor greenColor].CGColor;
maskLayer.path = endCycle.CGPath;
fromVC.view.layer.mask = maskLayer; //创建路径动画
CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
maskLayerAnimation.delegate = self;
maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
maskLayerAnimation.duration = [self transitionDuration:transitionContext];
maskLayerAnimation.delegate = self;
maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
[maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{ UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view]; //画两个圆路径// UIBezierPath *startCycle = [UIBezierPath bezierPathWithOvalInRect:fromVC.mapBtnFrame];
UIBezierPath *startCycle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 0, 0)]; CGFloat x = MAX(100, containerView.frame.size.width - 100); CGFloat y = MAX(100, containerView.frame.size.height - 100); CGFloat radius = sqrtf(pow(x, 2) + pow(y, 2)); UIBezierPath *endCycle = [UIBezierPath bezierPathWithArcCenter:containerView.center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES]; //创建CAShapeLayer进行遮盖
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = endCycle.CGPath; //将maskLayer作为toVC.View的遮盖
toVC.view.layer.mask = maskLayer; //创建路径动画
CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
maskLayerAnimation.delegate = self; //动画是加到layer上的,所以必须为CGPath,再将CGPath桥接为OC对象
maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
maskLayerAnimation.duration = [self transitionDuration:transitionContext];
maskLayerAnimation.delegate = self;
maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
[maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
switch (_type) {
case XWCircleSpreadTransitionTypePush:{
id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"]; [transitionContext completeTransition:YES];
UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
toView.layer.mask = nil;
UIViewController *vc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; vc.view.layer.mask = nil; }
break;
case XWCircleSpreadTransitionTypePop:{
id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
[transitionContext completeTransition:YES];
UIViewController *vc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
vc.view.layer.mask = nil; }
break;
}
}
@end
剩下的就是在ViewControllerA中实现代理<UINavigationControllerDelegate>中的animationControllerForOperation:
#pragma mark -
#pragma mark - UINavigation Delegate
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
if (fromVC == self) {
return [XWCircleSpreadTransition transitionWithTransitionType:XWCircleSpreadTransitionTypePush];
}
if (toVC == self) {
return [XWCircleSpreadTransition transitionWithTransitionType:XWCircleSpreadTransitionTypePop];
}
return nil;
}
到此,这个简单的转场动画已经实现完毕,而且动画效果可以自定义成自己想要的展现形式。
类似nike+、香蕉打卡的转场动画效果-b的更多相关文章
- iOS CATransition 自定义转场动画
https://www.jianshu.com/p/39c051cfe7dd CATransition CATransition 是CAAnimation的子类(如下图所示),用于控制器和控制器之间的 ...
- iOS 动画学习之视图控制器转场动画
一.概述 1.系统会创建一个转场相关的上下文对象,传递到动画执行器的animateTransition:和transitionDuration:方法,同样,也会传递到交互Controller的star ...
- 自己定义转场动画--Swift3.0版本号
转场动画这事.说简单也简单.能够通过presentViewController:animated:completion:和dismissViewControllerAnimated:completio ...
- ios基础动画、关键帧动画、动画组、转场动画等
概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...
- iOS自定义转场动画实战讲解
iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...
- 超酷炫的转场动画?CSS 轻松拿下!
在 WeGame 的 PC 端官网首页,有着非常多制作精良的基于滚动的动画效果. 这里我简单截取其中 2 个比较有意思的转场动画,大家感受感受.转场动画 1: 转场动画 2: 是不是挺有意思的,整个动 ...
- iOS开发UI篇—核心动画(转场动画和组动画)
转自:http://www.cnblogs.com/wendingding/p/3801454.html iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的 ...
- CATransition-转场动画
CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATrans ...
- iOS:核心动画之转场动画CATransition
转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 U ...
随机推荐
- sublime text修改TAB缩进为空格
在sublime text中将TAB缩进直接转化为4个空格,可以按照如下方式操作: 菜单栏: Preferences -> Settings – More -> Syntax Specif ...
- js函数大全
js函数集·字符串(String) 1.声明 var myString = new String("Every good boy does fine."); var myStrin ...
- getComputedStyle和currentStyle
/*alert(div.style.width)*/ //null function getstyle(obj,name){ if(obj.currentStyle) { return obj.cur ...
- MySQL数据库中的触发器
--触发器是一类特殊的监控增删改操作,并产生相应的增删改的操作 --1,监视谁 2,监视动作 3,监视时间(之前或之后) 4,触发的事件 --触发器的简单语法 create trigger 触发器名字 ...
- http协议的总结说明
关于http协议已经有很多大牛们的讨论,从他们的文章中获益匪浅,作为一个通信专业的学生,还是想从计算机网络的角度谈一下自己的认识.http协议全称超文本传输协议,是一种允许将超文本标记语言(HTML) ...
- 【TOMCAT】Tomcat gzip压缩传输数据
概述 由于我们项目的三维模型文件非常大,为了提高传输速度,在服务端对其做zip压缩处理非常有必要,能够极大的提高传输速度. 配置 首先需要修改web.xml中请求的数据文件的mime类型的mappin ...
- 修改msconfig->引导->高级选项-》最大内存为512M
本来想开机提速的!手贱 把 最大内存设置成了512M 结果开机悲剧了,启用了微软的自动修复也不能解决问题!最后是WIN7 PE系统下直接修复boot结果了.遇到这种问题的朋友们可以试试喔
- JSP之初识2
<与%之间不可有空格,但是后面可以有空格 <%@ page language="java" import="java.util.*" pageEnc ...
- xml初读
形式良好的 XML 文档 “形式良好”或“结构良好”的 XML 文档拥有正确的语法. “形式良好”(Well Formed)的 XML 文档会遵守前几章介绍过的 XML 语法规则: XML 文档必须有 ...
- js 比较两个日期的大小的例子
例子,直接比较大小即可 代码如下 复制代码 <script>var st="2009-10-20 14:38:40"var et="2009-10-20 15 ...