IOS开发基础篇--CAShapeLayer的strokeStart和strokeEnd属性
2016-02-15 01:12
597 查看
http://blog.csdn.net/yixiangboy/article/details/50662704
一、案例演示
最近有一个小需求,就是要做一个圆形进度条,大概样子如下:。
在不知道有CAShapeLayer的strokeStart和strokeEnd属性的时候,我采取的方法就是实时的 移除旧的CAShapeLayer 然后重绘这个圆形的CAShapeLayer。显然这种方式的效率是不高的。后来在一次看别人Demo的时候,发现别人使用了CAShapeLayer的strokeStart和strokeEnd属性,实现这一个效果十分的简单方便。下面就和大家来讲一讲这两个属性的使用。
二、属性详解
苹果官方给出这两个属性的解释为:/* These values define the subregion of the path used to draw the
* stroked outline. The values must be in the range [0,1] with zero
* representing the start of the path and one the end. Values in
* between zero and one are interpolated linearly along the path
* length. strokeStart defaults to zero and strokeEnd to one. Both are
* animatable. */
大概意思就是:我们可以对绘制的Path进行分区。这两个属性的值在0~1之间,0代表Path的开始位置,1代表Path的结束位置。是一种线性递增关系。strokeStart默认值为0,strokeEnd默认值为1。这两个属性都支持动画。
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; shapeLayer.lineWidth = 2.0f; shapeLayer.strokeColor = [UIColor redColor].CGColor; [_demoView.layer addSublayer:shapeLayer];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
我们通过以上代码设置:strokeStart=0.1f; strokeEnd=0.7f则显示如下图所示。
三、圆形进度条实现代码
综上所述我们知道,strokeStart和strokeEnd可以设置一条Path的起始和终止的位置,通过利用strokeStart和strokeEnd这两个属性支持动画的特点,我们通过以下代码就可以实现圆形进度条的效果。CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.frame = _demoView.bounds; UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:_demoView.bounds]; shapeLayer.path = path.CGPath; shapeLayer.fillColor = [UIColor clearColor].CGColor; 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"];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
四、联系方式
微博:新浪微博博客:http://blog.csdn.net/yixiangboy
github:https://github.com/yixiangboy
顶0
相关文章推荐
- iOS 字符属性NSAttributedString描述
- iOS显示“此证书的签发者无效”来破
- iOS 学习资料整理 {非常有用,强烈推荐}
- iOS 开发小记 (八)
- iOS ViewController生命周期
- 简单完美的解决键盘遮挡输入框的问题
- 蓝懿IOS多线程管理进程间访问
- iOS内存管理
- iOS 可移动View的实现
- iOS9新特性及其适配方案
- iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
- ios相对路径与绝对路径的转化NSBundle
- IOS之nil、Nil、NSULL、NULL之间的区别
- Cornerstone的创建分支Branch
- iOS学习之HelloWorld工程
- iOS 动态显示标签
- iOS - Core Graphics 之 Quartz2D
- iOS IM开发准备工作(一)XML解析
- iOS开发笔记-圆滑的曲线
- iOS字符串处理详解