类似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 ...
随机推荐
- 【解决】应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。
换了SSD硬盘,装了Windows 7 SP1 x64的系统.用了一段时间,突然一天有些软件打不开了.弹出下面的提示 应用程序无法正常启动(0xc000007b).请单击“确定”关闭应用程序.第一时间 ...
- Bootstrap之Footer页尾布局(2015年05月28日)
直接上页尾部分的代码: <!--采用container-fluid,使得整个页尾的宽度为100%,并设置它的背景色--><footer class="container-f ...
- Redis - 排序
SORT 命令格式 SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALP ...
- pdf文件流生成pdf文件
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...
- C#算法基础之递归排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- HDOJ2015偶数求和
偶数求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 每天一道LeetCode--169.Majority Elemen
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Ghost版Win8.1系统企业版下载
host版Win8.1系统企业版,下载完成后一定要使用校验工具验证GHO文件MD5值,如果不符请不要安装,不然安装失败后果自负.GHO文件路径一定不要带中文,否则无法安装.安装完成第一次进入桌面会黑屏 ...
- 断开SVN连接操作方法
SVN是日常项目管理中经常使用到的工具,然而拷贝备份需要与SVN服务器断开连接,具体操作步骤: 1.在桌面建立一个文本文件,取名为kill-svn-folders.reg(扩展名由txt改为reg), ...
- (四)Qt之右键菜单
1.右键菜单创建和显示 作为一种交互性强.使用方便的右键菜单在程序中是非常常用的,在Qt中可以轻松的实现. QMenu menu; //添加菜单项,指定图标.名称.响应函数 menu.addActio ...