一、CAShapeLayer简单介绍:

、CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值

2、CAShapeLayer须要与贝塞尔曲线配合使用才有意义

3、使用CAShapeLayer与贝塞尔曲线能够实现不在view的drawRect方法中画出一些想要的图形

4、CAShapeLayer属于CoreAnimation框架,其动画渲染直接提交到手机的GPU其中,相较于view的drawRect方法使用CPU渲染而言,其效率极高,能大大优化内存使用情况

五角星动画

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSTimer      *timer;

@property (nonatomic,strong) CAShapeLayer *shapeLayer;

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

//
创建shapeLayer

_shapeLayer = [CAShapeLayerlayer];

_shapeLayer.frame         = (CGRect){CGPointMake(,), CGSizeMake(,)};

_shapeLayer.position      =self.view.center;

_shapeLayer.path          = [selfgetStar1BezierPath].CGPath;

_shapeLayer.fillColor     = [UIColorclearColor].CGColor;

_shapeLayer.strokeColor   = [UIColorredColor].CGColor;

_shapeLayer.lineWidth     =2.f;

[self.view.layeraddSublayer:_shapeLayer];

//
创建定时器

_timer = [NSTimerscheduledTimerWithTimeInterval:1.f

target:self

selector:@selector(pathAnimation)

userInfo:nil

repeats:YES];

}

/**

*  运行path的动画

*/

- (void)pathAnimation {

;

==) {

CABasicAnimation *circleAnim = [CABasicAnimationanimationWithKeyPath:@"path"];

circleAnim.removedOnCompletion =NO;

circleAnim.;

circleAnim.fromValue           = (__bridgeid)[self getStar1BezierPath].CGPath;

circleAnim.toValue             = (__bridgeid)[self getStar2BezierPath].CGPath;

_shapeLayer.path               = [selfgetStar2BezierPath].CGPath;

[_shapeLayeraddAnimation:circleAnim forKey:@"animateCirclePath"];

}else {

CABasicAnimation *circleAnim = [CABasicAnimationanimationWithKeyPath:@"path"];

circleAnim.removedOnCompletion =NO;

circleAnim.;

circleAnim.fromValue           = (__bridgeid)[self getStar2BezierPath].CGPath;

circleAnim.toValue             = (__bridgeid)[self getStar1BezierPath].CGPath;

_shapeLayer.path               = [selfgetStar1BezierPath].CGPath;

[_shapeLayeraddAnimation:circleAnim forKey:@"animateCirclePath"];

}

}

/**

*  贝塞尔曲线1

*

*  @return 贝塞尔曲线

*/

-(UIBezierPath *)getStar1BezierPath {

//// Star Drawing

UIBezierPath* starPath = [UIBezierPathbezierPath];

[starPathmoveToPoint: CGPointMake(22.5,2.5)];

[starPath addLineToPoint:CGPointMake(28.32,14.49)];

[starPath addLineToPoint:CGPointMake(41.52,16.32)];

[starPath addLineToPoint:CGPointMake(31.92,25.56)];

[starPath addLineToPoint:CGPointMake(34.26,38.68)];

[starPath addLineToPoint:CGPointMake(22.5,32.4)];

[starPath addLineToPoint:CGPointMake(10.74,38.68)];

[starPath addLineToPoint:CGPointMake(13.08,25.56)];

[starPath addLineToPoint:CGPointMake(3.48,16.32)];

[starPath addLineToPoint:CGPointMake(16.68,14.49)];

[starPathclosePath];

return starPath;

}

/**

*  贝塞尔曲线2

*

*  @return 贝塞尔曲线

*/

-(UIBezierPath *)getStar2BezierPath {

//// Star Drawing

UIBezierPath* starPath = [UIBezierPathbezierPath];

[starPathmoveToPoint: CGPointMake(22.5,2.5)];

[starPath addLineToPoint:CGPointMake(32.15,9.21)];

[starPath addLineToPoint:CGPointMake(41.52,16.32)];

[starPath addLineToPoint:CGPointMake(38.12,27.57)];

[starPath addLineToPoint:CGPointMake(34.26,38.68)];

[starPath addLineToPoint:CGPointMake(22.5,38.92)];

[starPath addLineToPoint:CGPointMake(10.74,38.68)];

[starPath addLineToPoint:CGPointMake(6.88,27.57)];

[starPath addLineToPoint:CGPointMake(3.48,16.32)];

[starPath addLineToPoint:CGPointMake(12.85,9.21)];

[starPathclosePath];

return starPath;

}

@end

二、贝塞尔曲线与CAShapeLayer的关系

1、CAShapeLayer中有Shape这个单词。顾名思义,它须要一个形状才干生效

2、贝塞尔曲线能够创建基于矢量的路径

3、贝塞尔曲线给CAShapeLayer提供路径。CAShapeLayer在提供的路径中进行渲染。路径会闭环,所以路径绘制出了Shape

4、用于CAShapeLayer的贝塞尔曲线作为path,其path是一个首尾相接的闭环的曲线,即使该贝塞尔曲线不是一个闭环的曲线

注意:shape的frame
要大于BezierPath的frame.BezierPath不会由于share的frame而拉升。否则BezierPath截断

- (void)viewDidLoad {

[superviewDidLoad];

//创建椭圆形贝塞尔曲线

UIBezierPath *oval = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(,,
,)];

//创建矩形贝塞尔曲线

UIBezierPath *rect = [UIBezierPathbezierPathWithRect:CGRectMake(,,
,)];

//创建圆形贝塞尔曲线

UIBezierPath *circle = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(,,
,)];

//
创建CAShapeLayer

CAShapeLayer *shape = [CAShapeLayerlayer];

shape.frame         =CGRectMake(,,
, );

shape.position      =self.view.center;

//
显示CAShapeLayer的边界

shape.borderWidth   =1.f;

//
禁止内容显示超出CAShapeLayer的frame值

shape.masksToBounds =YES;

//改动贝塞尔曲线的填充颜色

shape.fillColor     = [UIColorredColor].CGColor;

//
建立贝塞尔曲线与CAShapeLayer之间的关联

shape.path = circle.CGPath;

//
加入并显示

[self.view.layeraddSublayer:shape];

}

三、StrokeStart与StrokeEnd动画

、将ShapeLayer的fillColor设置成透明背景

2、设置线条的宽度(lineWidth)的值

3、设置线条的颜色

、将strokeStart值设定成,然后让strokeEnd的值变化触发隐式动画

@interface ViewController ()

@property (nonatomic,strong) NSTimer      *timer;    
// 定时器

@property (nonatomic,strong) CAShapeLayer *shapeLayer;//
形状layer

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

//
设置背景色

];

//创建椭圆形贝塞尔曲线

UIBezierPath *oval        = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(,,
,)];

//
创建CAShapeLayer

_shapeLayer               = [CAShapeLayerlayer];

_shapeLayer.frame         =CGRectMake(,,
, );

_shapeLayer.position      =self.view.center;

//
改动CAShapeLayer的线条相关值

_shapeLayer.fillColor     = [UIColorclearColor].CGColor;

_shapeLayer.strokeColor   = [UIColorredColor].CGColor;

_shapeLayer.lineWidth     =2.f;

_shapeLayer.strokeStart   =0.f;

_shapeLayer.strokeEnd     =0.f;

//
建立贝塞尔曲线与CAShapeLayer之间的关联

_shapeLayer.path          = oval.CGPath;

//
加入并显示

[self.view.layeraddSublayer:_shapeLayer];

//
创建定时器

_timer = [NSTimerscheduledTimerWithTimeInterval:1.f

target:self

selector:@selector(animationEventTypeTwo)

userInfo:nil

repeats:YES];

}

/**

*  动画效果1

*/

- (void)animationEventTypeOne {

//
运行隐式动画

_shapeLayer.strokeEnd =arc4random() % /100.f;

}

/**

*  动画效果2

*/

- (void)animationEventTypeTwo {

CGFloat valueOne = arc4random() % /
100.f;

CGFloat valueTwo = arc4random() % /
100.f;

// storkeStar
与storkeEnd同一时候赋值。strokeStart要小于strokeEnd的值

//
运行隐式动画

_shapeLayer.strokeStart = valueOne < valueTwo ?

valueOne : valueTwo;

_shapeLayer.strokeEnd   = valueOne > valueTwo ?

valueOne : valueTwo;

}

@end

四、用CAShapeLayer实现圆形进度条效果

1、确定须要设定的參数

2、实现细节

3、进行測试

@interface CircleView :UIView

@property (nonatomic,assign) CGFloat  startValue;//
起始值(0~1)

@property (nonatomic,assign) CGFloat  lineWidth;
// 线宽(>0)

@property (nonatomic,strong) UIColor *lineColor;
// 线条颜色

@property (nonatomic,assign) CGFloat  value;    
// 变化的值

@end

#import "CircleView.h"

@interface CircleView ()

@property (nonatomic,strong)
CAShapeLayer *shapeLayer;

@end

@implementation CircleView

- (instancetype)initWithFrame:(CGRect)frame {

self = [superinitWithFrame:frame];

if (self) {

//
创建出CAShapeLayer

_shapeLayer       = [CAShapeLayerlayer];

_shapeLayer.frame =self.bounds;

//
创建出贝塞尔曲线

UIBezierPath *path = [UIBezierPathbezierPathWithOvalInRect:self.bounds];

//
贝塞尔曲线与CAShapeLayer产生关联

_shapeLayer.path = path.CGPath;

// 基本配置

_shapeLayer.fillColor   = [UIColorclearColor].CGColor;

_shapeLayer.lineWidth   =1.f;

_shapeLayer.strokeColor = [UIColorredColor].CGColor;

_shapeLayer.strokeEnd   =0.f;

//
加入到当前view

[self.layeraddSublayer:_shapeLayer];

}

return
self;

}

@synthesize startValue =_startValue;

- (void)setStartValue:(CGFloat)startValue {

_startValue           = startValue;

_shapeLayer.strokeEnd = startValue;

}

- (CGFloat)startValue {

return_startValue;

}

@synthesize lineWidth =_lineWidth;

- (void)setLineWidth:(CGFloat)lineWidth {

_lineWidth            = lineWidth;

_shapeLayer.lineWidth = lineWidth;

}

- (CGFloat)lineWidth {

return_lineWidth;

}

@synthesize lineColor =_lineColor;

- (void)setLineColor:(UIColor *)lineColor {

_lineColor              = lineColor;

_shapeLayer.strokeColor = lineColor.CGColor;

}

- (UIColor *)lineColor {

return_lineColor;

}

@synthesize value = _value;

- (void)setValue:(CGFloat)value {

_value                = value;

_shapeLayer.strokeEnd = value;

}

- (CGFloat)value {

return _value;

}

@end

#import "CircleView.h"

@interface
ViewController ()

{

CircleView *circle;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

circle             = [[CircleViewalloc] initWithFrame:CGRectMake(,,
, )];

circle.center      =self.view.center;

circle.startValue  =0.5;

circle.lineWidth   =3.f;

circle.lineColor   = [UIColorgrayColor];

[self.viewaddSubview:circle];

[selfperformSelector:@selector(delayAnimation)

withObject:nil

afterDelay:3.f];

}

- (void)delayAnimation {

circle.value =1.f;

}

@end

CAShapeLayer实现圆形进度条效果的更多相关文章

  1. html5 svg 圆形进度条

    html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  2. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  3. canvas 绘制圆形进度条

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 基于CAShapeLayer和贝塞尔曲线的圆形进度条动画

    通过CAShapeLayer和贝塞尔曲线搭配的方法,创建的简单的圆形进度条的教程先简单的介绍下CAShapeLayer1,CAShapeLayer继承自CALayer,可使用CALayer的所有属性2 ...

  5. 基于CAShapeLayer和贝塞尔曲线的圆形进度条动画【装载】

    初次接触CAShapeLayer和贝塞尔曲线,看了下极客学院的视频.对初学者来说感觉还不错.今天来说一个通过CAShapeLayer和贝塞尔曲线搭配的方法,创建的简单的圆形进度条的教程先简单的介绍下C ...

  6. HTML5效果:Canvas 实现圆形进度条并显示数字百分比

    实现效果 1.首先创建html代码 <canvas id="canvas" width="500" height="500" styl ...

  7. 用HTML、CSS、JS制作圆形进度条(无动画效果)

    逻辑 1.首先有一个圆:蓝色的纯净的圆,效果: 2.再来两个半圆,左边一个,右边一个将此蓝色的圆盖住,效果: 此时将右半圆旋转60°,就会漏出底圆,效果:   然后我们再用一个比底圆小的圆去覆盖这个大 ...

  8. WPF 实现圆形进度条

    项目中用到圆形进度条,首先就想到使用 ProgressBar 扩展一个,在园子里找到迷途的小榔头给出的思路和部分代码,自己加以实现. 进度小于60显示红色,大于60则显示绿色.效果如下: 基本思路: ...

  9. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

随机推荐

  1. 单元测试方法属性(Unit Test Method Attribute)

    Additional test attributes(可以在测试方法上使用的属性)As you have seen, the unit-testing subsystem within Visual ...

  2. DNX 概览

    来源https://docs.asp.net/en/latest/dnx/overview.html .NET Execution Environment是什么 .NET Execution Envi ...

  3. [置顶] kubernetes资源类型--Service

    为了适应快速的业务需求,微服务架构已经逐渐成为主流,微服务架构的应用需要有非常好的服务编排支持.K8S中的核心要素Service便提供了一套简化的服务代理和发现机制,天然适应微服务架构. 实现原理 S ...

  4. web服务器、wsgi、应用

    1.web服务器(比如Apache.Nginx.Tomcat.Jetty.IIS)属于静态服务器,只能返回静态的文件,服务器多是C/C++语言开发的.web服务器可以说一个request proxy, ...

  5. How to get the edited text from itext in fabricjs

    https://stackoverflow.com/questions/39286826/how-to-get-the-edited-text-from-itext-in-fabricjs http: ...

  6. vscode - 添加背景图片

    首先,Ctrl+Shift+P安装backround , 而后重启vscode会有默认的背景图片 修改背景图,可自定义三张 具体请看gif图 最开始时,发现png根本不是全透明,用ps处理了一下(下列 ...

  7. Linux下的各文件夹的作用(转)

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  8. cordova与ios native code交互的原理

    非常早曾经写了一篇博客,总结cordova插件怎么调用到原生代码:cordova调用过程,只是写得太水.基本没有提到原理.近期加深了一点理解,又一次补充说明一下 js调用native 以下是我们产品中 ...

  9. Eclipse 使用 SVN 插件后改动用户方法汇总

    判定 SVN 插件是哪个 JavaH 的处理方法 SVNKit 的处理方法 工具自带改动功能 删除缓存的秘钥文件 其他发表地点 判定 SVN 插件是哪个 常见的 Eclipse SVN 插件我知道的一 ...

  10. mysql innodb_data_file_path配置增加

    在配置innodb_data_file_path=ibdata1:200M:autoextend时,想增加空间,默认文件不断增加直到200M,如果要增加大小,直接修改参数增加大小会报错: auto-e ...