iOS关于CAShapeLayer与UIBezierPath的知识内容
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 。
1:UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个OC封装。使用此类可以定义常见的圆形、多边形等形状 。我们使用直线、弧(arc)来创建复杂的曲线形状。每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。
2:CAShapeLayer: CAShapeLayer顾名思义,继承于CALayer。 每个CAShapeLayer对象都代表着将要被渲染到屏幕上的一个任意的形状(shape)。具体的形状由其path(类型为CGPathRef)属性指定。 普通的CALayer是矩形,所以需要frame属性。CAShapeLayer初始化时也需要指定frame值,但 它本身没有形状,它的形状来源于其属性path 。CAShapeLayer有不同于CALayer的属性,它从CALayer继承而来的属性在绘制时是不起作用的。
实例1:画一个圆形
- (void)viewDidLoad {
[super viewDidLoad]; //创建出CAShapeLayer
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = CGRectMake(, , , );//设置shapeLayer的尺寸和位置
self.shapeLayer.position = self.view.center;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor //设置线条的宽度和颜色
self.shapeLayer.lineWidth = 1.0f;
self.shapeLayer.strokeColor = [UIColor redColor].CGColor; //创建出圆形贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; //让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayer.path = circlePath.CGPath; //添加并显示
[self.view.layer addSublayer:self.shapeLayer];
}
现在我们要用到CAShapeLayer的两个参数,strokeEnd和strokeStart
Stroke:用笔画的意思
在这里就是起始笔和结束笔的位置
Stroke为1的话就是一整圈,0.5就是半圈,0.25就是1/4圈。以此类推
如果我们把起点设为0,终点设为0.75
//设置stroke起始点
self.shapeLayer.strokeStart = 0;
self.shapeLayer.strokeEnd = 0.75;
实例2:画两个圆,其中一个圆表示进度
//画两个圆形
-(void)createBezierPath:(CGRect)mybound
{
//外圆
_trackPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width - 0.7)/ startAngle: endAngle:M_PI * clockwise:YES];; _trackLayer = [CAShapeLayer new];
[self.view.layer addSublayer:_trackLayer];
_trackLayer.fillColor = nil;
_trackLayer.strokeColor=[UIColor grayColor].CGColor;
_trackLayer.path = _trackPath.CGPath;
_trackLayer.lineWidth=;
_trackLayer.frame = mybound; //内圆
_progressPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:(mybound.size.width - 0.7)/ startAngle:- M_PI_2 endAngle:(M_PI * ) * 0.7 - M_PI_2 clockwise:YES]; _progressLayer = [CAShapeLayer new];
[self.view.layer addSublayer:_progressLayer];
_progressLayer.fillColor = nil;
_progressLayer.strokeColor=[UIColor redColor].CGColor;
_progressLayer.lineCap = kCALineCapRound;
_progressLayer.path = _progressPath.CGPath;
_progressLayer.lineWidth=;
_progressLayer.frame = mybound;
}
实例3:创建一个转动的圆
- (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor=[UIColor whiteColor]; [self circleBezierPath];
//用定时器模拟数值输入的情况
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(circleAnimationTypeOne)
userInfo:nil
repeats:YES];
} -(void)circleBezierPath
{
//创建出CAShapeLayer
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = CGRectMake(, , , );
self.shapeLayer.position = self.view.center;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor; //设置线条的宽度和颜色
self.shapeLayer.lineWidth = 2.0f;
self.shapeLayer.strokeColor = [UIColor redColor].CGColor; //设置stroke起始点
self.shapeLayer.strokeStart = ;
self.shapeLayer.strokeEnd = ; //创建出圆形贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; //让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayer.path = circlePath.CGPath; //添加并显示
[self.view.layer addSublayer:self.shapeLayer];
} - (void)circleAnimationTypeOne
{
if (self.shapeLayer.strokeEnd > && self.shapeLayer.strokeStart < ) {
self.shapeLayer.strokeStart += 0.1;
}else if(self.shapeLayer.strokeStart == ){
self.shapeLayer.strokeEnd += 0.1;
} if (self.shapeLayer.strokeEnd == ) {
self.shapeLayer.strokeStart = ;
} if (self.shapeLayer.strokeStart == self.shapeLayer.strokeEnd) {
self.shapeLayer.strokeEnd = ;
}
} - (void)circleAnimationTypeTwo
{
CGFloat valueOne = arc4random() % / 100.0f;
CGFloat valueTwo = arc4random() % / 100.0f; self.shapeLayer.strokeStart = valueOne < valueTwo ? valueOne : valueTwo;
self.shapeLayer.strokeEnd = valueTwo > valueOne ? valueTwo : valueOne;
}
实例4:通过点画线组成一个五边线
//画一个五边形
-(void)fiveAnimation
{
UIBezierPath *aPath = [UIBezierPath bezierPath];
//开始点 从上左下右的点
[aPath moveToPoint:CGPointMake(,)];
//划线点
[aPath addLineToPoint:CGPointMake(, )];
[aPath addLineToPoint:CGPointMake(, )];
[aPath addLineToPoint:CGPointMake(, )];
[aPath addLineToPoint:CGPointMake(, )];
[aPath closePath];
//设置定点是个5*5的小圆形(自己加的)
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-/2.0, , , )];
[aPath appendPath:path]; CAShapeLayer *shapelayer = [CAShapeLayer layer];
//设置边框颜色
shapelayer.strokeColor = [[UIColor redColor]CGColor];
//设置填充颜色 如果只要边 可以把里面设置成[UIColor ClearColor]
shapelayer.fillColor = [[UIColor blueColor]CGColor];
//就是这句话在关联彼此(UIBezierPath和CAShapeLayer):
shapelayer.path = aPath.CGPath;
[self.view.layer addSublayer:shapelayer];
}
实例5:画一条虚线
//画一条虚线
-(void)createDottedLine
{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:self.view.bounds];
[shapeLayer setPosition:self.view.center];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]]; // 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:[[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:1.0f] CGColor]]; // 3.0f设置虚线的宽度
[shapeLayer setLineWidth:1.0f];
[shapeLayer setLineJoin:kCALineJoinRound]; // 3=线的宽度 1=每条线的间距
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSNumber numberWithInt:],
[NSNumber numberWithInt:],nil]]; // Setup the path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, , );
CGPathAddLineToPoint(path, NULL, ,); [shapeLayer setPath:path];
CGPathRelease(path); // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
[[self.view layer] addSublayer:shapeLayer];
}
实例6:画一个弧线
//画一个弧线
-(void)createCurvedLine
{
UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 5.0;
aPath.lineCapStyle = kCGLineCapRound; //线条拐角
aPath.lineJoinStyle = kCGLineCapRound; //终点处理
[aPath moveToPoint:CGPointMake(, )];
[aPath addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )]; self.CurvedLineLayer=[CAShapeLayer layer];
self.CurvedLineLayer.path=aPath.CGPath;
[self.view.layer addSublayer:self.CurvedLineLayer];
}
最近有个妹子弄的一个关于扩大眼界跟内含的订阅号,每天都会更新一些深度内容,在这里如果你感兴趣也可以关注一下(嘿对美女跟知识感兴趣),当然可以关注后输入:github 会有我的微信号,如果有问题你也可以在那找到我;当然不感兴趣无视此信息;
iOS关于CAShapeLayer与UIBezierPath的知识内容的更多相关文章
- ReactiveCocoa基础知识内容
本文记录一些关于学习ReactiveCocoa基础知识内容,对于ReactiveCocoa相关的概念如果不了解可以网上搜索:RACSignal有很多方法可以来订阅不同的事件类型,ReactiveCoc ...
- iOS仿京东分类菜单之UICollectionView内容
在上<iOS仿京东分类菜单实例实现>已经实现了大部分主体的功能,本文是针对右边集合列表进行修改扩展,使它达到分组的效果,本文涉及到的主要是UICollectionView的知识内容,左边列 ...
- 使用CAShapeLayer与UIBezierPath画出想要的图形
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 步骤: 1.新建UIBezierPath对象bezierPath 2.新建CAS ...
- 使用CAShapeLayer和UIBezierPath画一个自定义半圆弧button
通常我们使用系统自带的UIButton时,一般都是Rect矩形形式的,或则美工给出一张半圆弧的按钮,如图为一张半圆加三角形的按钮,而此时,如果给按钮添加点击事件时,响应事件依然为矩形区域,不符合我们的 ...
- 038改变状态栏的颜色(扩展知识:关于iOS不同版本的消息通知知识)
效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @e ...
- iOS开发 贝塞尔曲线UIBezierPath(2)
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- iOS开发 贝塞尔曲线UIBezierPath(后记)
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- IOS CAShapeLayer CAGradientLayer UIBezierPath 使用实例
CGRect rect = CGRectMake(100, 100, 100, 100); UIView * bgView = [[UIView alloc]initWithFrame:rect]; ...
- iOS:runtime最全的知识总结
runtime 完整总结 好东西,应该拿出来与大家分享... 南峰子博客地址:http://southpeak.github.io/blog/categories/ios/ 原文链接:http://w ...
随机推荐
- 索引深入浅出(4/10):非聚集索引的B树结构在聚集表
一个表只能有一个聚集索引,数据行以此聚集索引的顺序进行存储,一个表却能有多个非聚集索引.我们已经讨论了聚集索引的结构,这篇我们会看下非聚集索引结构. 非聚集索引的逻辑呈现 简单来说,非聚集索引是表的子 ...
- rsync密码使用要注意的几个点
1 客户端和服务端的密码文件不一样 服务端的密码文件格式是: 用户名:密码 比如: yejianfeng:123456 客户端的密码文件格式是: 密码: 比如 123456 所以如果一个机器即 ...
- 基于HT for Web的3D呈现A* Search Algorithm
最近搞个游戏遇到最短路径的常规游戏问题,正巧看到老同事写的3D机房最短路径巡线文章,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现,算法基于开源 https://github.c ...
- QWebView在 Qt 5.x中编译出错:File not found: main.obj
错误现象 近日由于项目需要,想要学习一下QWebView的使用.于是简单的建立了一个Qt工程,并编写了如下代码: #include <QApplication> #include < ...
- MySql安装出现问题---无服务,修改密码
之前在项目中一直使用的是SqlServer数据库,现在学习下MySql,下面是学习MySql的学习笔记. MySql简介 MySql是一种关联数据库管理系统(关系型),将数据 ...
- C# 中反射获取某类的子类和根据类型名动态创建对象
有时候,为了快速批量处理已经实现某个基类或者某个接口的子类,需要通过反射的方式获取到他们的类类型(Type),然后再通过 1 Activator.CreateInstance(objType); 或者 ...
- 15天玩转redis —— 第四篇 哈希对象类型
redis中的hash也是我们使用中的高频数据结构,它的构造基本上和编程语言中的HashTable,Dictionary大同小异,如果大家往后有什么逻辑需要用 Dictionary存放的话,可以根据场 ...
- 鼠标滑动加载div
- Android短信Notification的几个ID
private static final int NOTIFICATION_ID = 123; public static final int MESSAGE_FAILED_NOTIFICATION_ ...
- [教学] 将 Form 内的控件变成 Style 简易运用
1. 在 Form 上放一个 TImage ,再一个 TText 到 Image 里面,并将 Image1.StyleName 设定为 BtnStyle,如下: 2.接着放一个 TButton,将 S ...