博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
类似nike+、香蕉打卡的转场动画效果-b
阅读量:6338 次
发布时间:2019-06-22

本文共 7167 字,大约阅读时间需要 23 分钟。

首先,支持并感谢@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 
typedef NS_ENUM(NSUInteger, XWCircleSpreadTransitionType) {        XWCircleSpreadTransitionTypePush = 0,        XWCircleSpreadTransitionTypePop };@interface XWCircleSpreadTransition : NSObject
@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
)transitionContext{        return 0.7; }  - (void)animateTransition:(id
)transitionContext{        switch (_type) {                case XWCircleSpreadTransitionTypePush:                        [self presentAnimation:transitionContext];                        break;                case XWCircleSpreadTransitionTypePop:                        [self dismissAnimation:transitionContext];                        break;         } }  - (void)dismissAnimation:(id
)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
)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
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
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
)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; }
 

到此,这个简单的转场动画已经实现完毕,而且动画效果可以自定义成自己想要的展现形式。

转载地址:http://nhxoa.baihongyu.com/

你可能感兴趣的文章
select2和bootstrap模态框使用时出现的bug以及解决方案
查看>>
EOS cleos 链接节点命令
查看>>
4.39-Nginx日志不记录静态文件
查看>>
写出企业要求的 Python 代码风格
查看>>
OSChina 周四乱弹 —— 如何正确地请客吃饭
查看>>
OSChina 周三乱弹 ——所有的酒,都不如你
查看>>
Pig的输入输出及foreach,group关系操作
查看>>
TechParty - Code For Public - sz
查看>>
emacs 前端插件推荐[emmet-mode]
查看>>
dnsmasq配置文件
查看>>
Unity链接SqlServer数据库并进行简单的数据查询
查看>>
23种设计模式
查看>>
原生javascript学习:用循环改变div颜色
查看>>
ABBYY FineReader 12内置的自动化任务
查看>>
ab 测试 和 apache 修改 并发数 mpm
查看>>
Nginx 的软件负载均衡详解
查看>>
TIMED OUT WAITING FOR OHASD MONITOR
查看>>
有关Last-Modified 与 If-Modified-Since
查看>>
Tomcat 内存溢出,堆栈配置各种调整
查看>>
过滤器
查看>>