iOS开发:CAShapeLayer画圈:strokeEnd

说明:动画只是一个对layer的过程按帧显示,事实上frame是一次变化的

原理:使用UIBezierPath创建路径(一个整圆)+CAShapeLayer的strokeEnd属性 (结束点+动画)-->得到动画画圈效果

动画效果



1、属性说明

CAShapeLayer.h

@property CGFloat strokeStart;//画圈起点,默认0,支持动画

@property CGFloat strokeEnd;//画圈结束点,默认1,支持动画



2、代码部分(属性显示:不带动画)

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.frame = _demoView.bounds;

shapeLayer.strokeEnd = 0.7f;//结束点

shapeLayer.strokeStart = 0.1f;//开始点

//得到路径

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:_demoView.bounds];

shapeLayer.path = path.CGPath;

shapeLayer.fillColor = [UIColor clearColor].CGColor; //Layer 的填充色

shapeLayer.lineWidth = 2.0f;//宽度

shapeLayer.strokeColor = [UIColor redColor].CGColor;//边框色

//将CAShaperLayer放到某个层上显示

[_demoView.layer addSublayer:shapeLayer];

我们通过以上代码设置:strokeStart=0.1f; strokeEnd=0.7f


3、实现圆形进度条的实现代码(动画)

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.frame = _demoView.bounds;

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:_demoView.bounds];

shapeLayer.path = path.CGPath;

shapeLayer.fillColor = [UIColor clearColor].CGColor;//Layer 的填充色

shapeLayer.lineWidth = 2.0f;//宽度

shapeLayer.strokeColor = [UIColor redColor].CGColor;//边框色

[_demoView.layer addSublayer:shapeLayer];

CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

pathAnima.duration = 3.0f;//动画时间

pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];//开始点

pathAnima.toValue = [NSNumber numberWithFloat:1.0f];//结束点

pathAnima.fillMode = kCAFillModeForwards;

pathAnima.removedOnCompletion = NO;

[shapeLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];

我们通过以上代码设置:strokeStart=0.1f; strokeEnd=1.0f,带动画效果


CABasicAnimation的“转圈”动画


引导页的跳过


1、确定动画轨迹

CGFloat w = CGRectGetWidth(frame);

CGFloat h = CGRectGetHeight(frame);

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(w/2, h/2)radius:MIN(w, h)/2 startAngle:-M_PI_2  endAngle:3 * M_PI_2  clockwise:YES];

2、创建动画

// 倒计时的时间

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];

animation.duration = 3;

animation.fromValue = @(0.f);

animation.toValue = @(1.f);

animation.removedOnCompletion = NO;

animation.fillMode = kCAFillModeBoth;

3、CAShapeLayer添加动画

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

shapeLayer.fillColor = [UIColor clearColor].CGColor;

shapeLayer.strokeColor = [UIColor colorWithRed:0.02f green:0.69f blue:1.00f alpha:1.00f].CGColor;

shapeLayer.lineWidth = 1.0;

[self.layer addSublayer:shapeLayer];

shapeLayer.path = path.CGPath;

[shapeLayer addAnimation:animation forKey:nil];


CABasicAnimation使用总结UIBezierPath介绍, CAAnimation——基本动画,关键帧动画和贝塞尔路径

学无止境,做个记录

2017-01-12-SXH

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,722评论 6 30
  • #import "ChangeAnimationView.h" ``` @interface ChangeAnim...
    JinHuiZhang阅读 3,707评论 0 0
  • 实现效果图如下: 一.创建一个贝塞尔曲线 1.基础方法,先创建UIBezierPath对象,在添加路径 + (in...
    2897275c8a00阅读 6,197评论 0 4
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 9,008评论 7 11
  • 下午和女儿做手工“小阿福”(不倒翁),从冰箱取出鸡蛋,女儿从百宝盒里取出胶水,双面胶,彩纸彩笔,我小心翼翼的帮着她...
    白卉阅读 5,966评论 0 8

友情链接更多精彩内容