「 iOS 」CAShapeLayer转场动画

鲁芝
• 阅读 1741

「 iOS 」CAShapeLayer转场动画

在这个demo中,核心为选用画布CAShapeLayer,因为一般都是用它来处理形状之类的动画,结合了贝塞尔曲线来控制路径,然后使用CABasicAnimation核心动画来产生所有效果。

首先封装一个自定义的动画。
///动画自定义封装
-(void)animationWithView:(UIView *)view{
    //1.创建layer
    CAShapeLayer *layer = [[CAShapeLayer alloc]init];
    
    //2.创建贝塞尔路径(参数为圆的外接矩形)
    //间距
    CGFloat margin = 20;
    //半径
    CGFloat radius = 25;
    //屏幕尺寸
    CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;
    //屏幕高度
    CGFloat viewHeight = [UIScreen mainScreen].bounds.size.height;
    //屏幕对角线
    CGFloat endRadius =sqrt(viewHeight*viewHeight +viewWidth*viewWidth);
    
    //起始路径
    CGRect startRect = CGRectMake(viewWidth-2*radius-margin, margin, radius*2, radius*2);
    UIBezierPath *startPath = [UIBezierPath bezierPathWithOvalInRect:startRect];
    //终止路径
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(startRect, -endRadius, -endRadius) ];
    
    //3.设置贝塞尔属性
    //填充颜色
    layer.fillColor = [UIColor redColor].CGColor;
    //4.将贝塞尔作为layer的路径
    layer.path = startPath.CGPath;
    //将layer作为父视图的遮罩图层.
    view.layer.mask = layer;
    //5.将path添加到视图中
    //[self.view.layer addSublayer:layer];
    
    //使用核心动画实现
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    //这个属性是判断是动画之前还是动画之后的。
    if (self.isPresent) {
        animation.fromValue = (__bridge id _Nullable)(startPath.CGPath);
        animation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    }else{
        animation.fromValue = (__bridge id _Nullable)(endPath.CGPath);
        animation.toValue = (__bridge id _Nullable)(startPath.CGPath);
    }
    animation.delegate = self;
    
    //设置动画属性
    animation.fillMode = kCAFillModeForwards;
    
    animation.duration = 2;
    
    animation.removedOnCompletion = NO;
    
    //添加动画到图层
    [layer addAnimation:animation forKey:nil];
}

! 这里要注意这个mask的属性,设置之后就不需要再额外的add进去,它是一种用于遮罩视图的效果,并且设置的颜色会失效

在这个动画中,有三个重要的属性,号称“转场三剑客”。

UIViewControllerAnimatedTransitioning,主要负责转场的动画时间和动画具体内容。

#pragma mark - UIViewControllerAnimatedTransitioning
///转场动画时间
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 2;
}
///转场动画的内容
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
    
    //1.获取上下文的容器视图
    UIView *containerView = transitionContext.containerView;
    //2.获取目标视图
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    //3.将目标视图添加到容器视图
    UIView *aniView = self.isPresent?toView:fromView;
    
    [containerView addSubview:aniView];
    
    //4.开始动画
    [self animationWithView:aniView];
    
    
    
    self.context = transitionContext;
}

CAAnimationDelegate,主要负责监控动画开始和动画结束之后。

#pragma mark - CAAnimationDelegate
///动画开始
- (void)animationDidStart:(CAAnimation *)anim{
    
}

///每次动画结束调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    //5.告诉上下文完成转场,否则上下文将会一直等待系统通知是否完成.
    [self.context completeTransition:YES];
}

UIViewControllerTransitioningDelegate,主要负责告诉系统由哪个控制器提供转场,哪个控制器来解除转场。

#pragma mark - UIViewControllerTransitioningDelegate
///告诉由谁提供转场
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    
    self.isPresent = YES;
    return self;
}
///由谁解除转场
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    self.isPresent = NO;
    return self;
}

最后只需要在需要转场的控制器中使用这个封装的类即可

-(void)awakeFromNib{
    [super awakeFromNib];
    
    //1.设置跳转样式
    self.modalPresentationStyle = UIModalPresentationCustom;
    //2.设置代理
    self.animation = [[JanCustomAnimation alloc]init];
    
    self.transitioningDelegate = self.animation;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}

需要注意的是设置样式和代理,必须要优先于viewdidload之前设置,因为这里涉及到控制器的生命周期的问题

好了,到这里就可以实现完整的自定义转场的酷炫效果了。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
PhoneGap设置Icon
参考:http://cordova.apache.org/docs/en/latest/config\_ref/images.html通过config.xml中的<icon标签来设置Icon<iconsrc"res/ios/icon.png"platform"ios"width"57"height"57"densi
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
3年前
JS 苹果手机日期显示NaN问题
问题描述newDate("2019122910:30:00")在IOS下显示为NaN原因分析带的日期IOS下存在兼容问题解决方法字符串替换letdateStr"2019122910:30:00";datedateStr.repl
Stella981 Stella981
3年前
Flutter 之贝塞尔曲线(一)
贝塞尔曲线简介!(https://oscimg.oschina.net/oscnet/863784996212c918a1feef7a916bce28f31.png"bezier1.png")bezier1.png由上图可以看出:A,C依据控制点B不断的取点使得AD:ABBE:BCDF:DE,构成一个二阶贝塞尔曲线。AD:
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
3年前
Flutter贝塞尔曲线之水波纹与球形进度(二)
续上篇,再用贝塞尔曲线绘制一个循环水波纹,一个水波纹进度球,先看效果,以下效果的实现用的都是二阶贝塞尔曲线。效果图!(https://oscimg.oschina.net/oscnet/db689ecfebdfa10dbc59b588184913f7c6f.gif"bezierShow4.gif")bezierShow4.
Stella981 Stella981
3年前
IOS使用AVFoundation在视频上添加字幕以及控制字幕时间
IOS在视频上添加字幕效果的基本思路是:1.使用自定义的CATextLayer文字图层或者CAShapeLayer文字图层,添加到视频的Layer上创建用户自定义的字幕效果。这两者的区别是:CATextLayer支持设置简单的文字效果,包括文字的内容、字体、字号大小、对其方式、文字颜色、背景颜色等基本的属性;CAShapeLayer
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这