首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcathttps://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的更多相关文章

  1. iOS CATransition 自定义转场动画

    https://www.jianshu.com/p/39c051cfe7dd CATransition CATransition 是CAAnimation的子类(如下图所示),用于控制器和控制器之间的 ...

  2. iOS 动画学习之视图控制器转场动画

    一.概述 1.系统会创建一个转场相关的上下文对象,传递到动画执行器的animateTransition:和transitionDuration:方法,同样,也会传递到交互Controller的star ...

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

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

  4. ios基础动画、关键帧动画、动画组、转场动画等

    概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...

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

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

  6. 超酷炫的转场动画?CSS 轻松拿下!

    在 WeGame 的 PC 端官网首页,有着非常多制作精良的基于滚动的动画效果. 这里我简单截取其中 2 个比较有意思的转场动画,大家感受感受.转场动画 1: 转场动画 2: 是不是挺有意思的,整个动 ...

  7. iOS开发UI篇—核心动画(转场动画和组动画)

    转自:http://www.cnblogs.com/wendingding/p/3801454.html iOS开发UI篇—核心动画(转场动画和组动画) 一.转场动画简单介绍 CAAnimation的 ...

  8. CATransition-转场动画

    CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATrans ...

  9. iOS:核心动画之转场动画CATransition

    转场动画——CATransition CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 U ...

随机推荐

  1. 关于Eclipse的Save时的自定义操作

    Eclipse中Save+S的时候会包含很多其他操作.例如代码格式化,自动导入或者删除jar包等等. 如果修改代码的时候不想让Eclipse做这些操作,有两种方法. 第一: 用一个文本编辑器打开这个文 ...

  2. Ubuntu下用SecureCRT连接串口/dev/ttyUSB0权限修复

    在普通用户的模式下,用SecureCRT链接串口交换机,开始会提示/dev/ttyUSB0权限不足,无法打开,临时的解决办法是 chmod 0+rw /dev/ttyUSB0 但是这个重启后便没了作用 ...

  3. hdu 3622 二分+2-SAT判定

    思路:如题 #include<iostream> #include<algorithm> #include<cstring> #include<cstdio& ...

  4. java-MySQL存储过程

    import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import ...

  5. Sublime Text3 包管理器、插件安装

    安装插件之前先要安装包管理器,包管理器的安装也很简单,复制粘贴对应版本命令代码回车即可 一.包管理器安装 1.打开Sublime3控制台,按ctrl+~ 2.输入安装包管理器命令行代码 3.注意需要联 ...

  6. 媒体查询的应用以及在css3中的变革

    CSS一直都支持设置与媒体相关联的样式表.它们可以适应不同媒体类型的显示.例如,文档在屏幕显示时使用sans-serif字体,在打印时则使用serif字体.screen和print是两种预定义的媒体类 ...

  7. Redis 命令 - Hashs

    HDEL key field [field ...] Delete one or more hash fields 127.0.0.1:6379> HSET book.1 title hello ...

  8. 每天一道LeetCode--342. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  9. PHP 和Apache的安装和配置

    1. apache官网安装指南: http://jingyan.baidu.com/article/29697b912f6539ab20de3cf8.html 2. 官方安装包下载地址: http:/ ...

  10. OC 成员变量作用域

    1.  成员变量作用域:    @public :在任何地方都能直接访问对象的成员变量.    @private :只能在当前类的对象方法中直接访问.(@implementation中默认是@priv ...