iOS之CGPath的应用(二)
1、矩形路径
CG_EXTERN CGPathRef CGPathCreateWithRect(CGRect rect,
const CGAffineTransform * __nullable transform)
- (void)createWithRect{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
firstLayer.path = CGPathCreateWithRect(CGRectMake(, , , ), nil);
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
firstLayer.lineWidth = ;
[self.showView.layer addSublayer:firstLayer];
self.pathRect.text = @"路径frame(90, 90, 20, 20)";
}ForeverGuard博客园
效果图
2、椭圆路径
CG_EXTERN CGPathRef CGPathCreateWithEllipseInRect(CGRect rect,
const CGAffineTransform * __nullable transform)
//CGPathCreateWithEllipseInRect
//以矩形四边相切画圆,矩形宽高相等就是园
- (void)createWithEllipseInRect{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
firstLayer.path = CGPathCreateWithEllipseInRect(CGRectMake(, , ,), nil);
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
firstLayer.lineWidth = ;
[self.showView.layer addSublayer:firstLayer];
self.pathRect.text = @"路径frame(70, 50, 60,100)";
}
效果图
3、圆角矩形
CG_EXTERN CGPathRef CGPathCreateWithRoundedRect(CGRect rect,
CGFloat cornerWidth, CGFloat cornerHeight,
const CGAffineTransform * __nullable transform)
CG_EXTERN void CGPathAddRoundedRect(CGMutablePathRef cg_nullable path,
const CGAffineTransform * __nullable transform, CGRect rect,
CGFloat cornerWidth, CGFloat cornerHeight)
//CGPathCreateWithRoundedRect
- (void)createWithRoundedRect{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
firstLayer.path = CGPathCreateWithRoundedRect(CGRectMake(, , , ), , , nil);
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
firstLayer.lineWidth = ;
[self.showView.layer addSublayer:firstLayer];
self.pathRect.text = @"路径frame(10,50 , 180, 100)";
}
//CGPathAddRoundedRect
- (void)addRoundedRect{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
CGMutablePathRef wavePath = CGPathCreateMutable();
CGPathAddRoundedRect(wavePath, nil, CGRectMake(, , , ), ,);
firstLayer.path = wavePath;
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
firstLayer.lineWidth = ;
[self.showView.layer addSublayer:firstLayer];
self.pathRect.text = @"路径frame(10,50 , 180, 100)";
}
效果图
4、虚线路径
CG_EXTERN CGPathRef __nullable CGPathCreateCopyByDashingPath(
CGPathRef cg_nullable path, const CGAffineTransform * __nullable transform,
CGFloat phase, const CGFloat * __nullable lengths, size_t count)
//CGPathCreateCopyByDashingPath
-(void)createCopyByDashingPath{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
CGMutablePathRef wavePath = CGPathCreateMutable();
CGPathAddRoundedRect(wavePath, nil, CGRectMake(, , , ), ,);
CGFloat floats[] = {,,,};//可以自行设置多组
firstLayer.path = CGPathCreateCopyByDashingPath(wavePath, nil, , floats, );
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
firstLayer.lineWidth = ;
[self.showView.layer addSublayer:firstLayer];
self.pathRect.text = @"路径frame(10,50 , 180, 100)";
}
效果图
5、斜线
CG_EXTERN CGPathRef __nullable CGPathCreateCopyByStrokingPath(
CGPathRef cg_nullable path, const CGAffineTransform * __nullable transform,
CGFloat lineWidth, CGLineCap lineCap,
CGLineJoin lineJoin, CGFloat miterLimit)
//CGPathCreateCopyByStrokingPath
//typedef CF_ENUM(int32_t, CGLineJoin) {
// kCGLineJoinMiter, 锋利
// kCGLineJoinRound, 圆角
// kCGLineJoinBevel 贝塞尔风格
//};
//
//typedef CF_ENUM(int32_t, CGLineCap) {
// kCGLineCapButt, 线冒精确到点(默认)
// kCGLineCapRound, 线冒为半径为线宽一半的圆弧
// kCGLineCapSquare 线冒尖锐的过渡
//};
- (void)createCopyByStrokingPath{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
CGMutablePathRef wavePath = CGPathCreateMutable();
CGPathMoveToPoint(wavePath, nil, ,*.);
CGPathAddLineToPoint(wavePath, nil, self.showView.frame.size.width- , *0.5);
CGPathAddLineToPoint(wavePath, nil, self.showView.frame.size.width-, );
CGPathAddLineToPoint(wavePath, nil, , );
firstLayer.path = CGPathCreateCopyByStrokingPath(wavePath, nil,, kCGLineCapRound, kCGLineJoinRound, );
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
[self.showView.layer addSublayer:firstLayer];
}
效果图
6、其它划线
- (void)linePath{
CAShapeLayer *firstLayer = [CAShapeLayer layer];
CGMutablePathRef wavePath = CGPathCreateMutable();
// 确定路径起点
CGPathMoveToPoint(wavePath, nil, ,);
// 画一条直线
CGPathAddLineToPoint(wavePath, nil, , );
// 添加一段二次贝塞尔曲线
CGPathAddQuadCurveToPoint(wavePath, nil, , , , );
// 添加一段三次贝塞尔曲线
CGPathAddCurveToPoint(wavePath, nil, , , , , , );
// 追加一个矩形
CGPathAddRect(wavePath, nil, CGRectMake(, , , ));
// 追加一组矩形
CGRect rects[] = {CGRectMake(, , , ),CGRectMake(, , , )};
CGPathAddRects(wavePath, nil, rects, );
// 追加一组线条
CGPoint points[] = {CGPointMake(, ),CGPointMake(, ),CGPointMake(, )};
CGPathAddLines(wavePath, nil, points, );
// 追加一个椭圆
CGPathAddEllipseInRect(wavePath, nil, CGRectMake(, , , ));
// 追加一段圆弧
CGPathAddRelativeArc(wavePath, nil, , , , , M_PI_4);
// 追加一段圆弧
CGPathAddArc(wavePath, nil, , , , , M_PI, NO);
// 追加一段相切弧
CGPathAddArcToPoint(wavePath, nil, , , , , );
firstLayer.path = wavePath;
firstLayer.fillColor = [UIColor redColor].CGColor;
firstLayer.strokeColor = [UIColor blueColor].CGColor;
[self.showView.layer addSublayer:firstLayer];
}
效果图
iOS之CGPath的应用(二)的更多相关文章
- iOS如何获取网络图片(二)
ios如何获取图片(二)无沙盒下 解决问题 *解决问题1:tableView滑动卡顿,图片延时加载 解决方法:添加异步请求,在子线程里请求网络,在主线程刷新UI *解决问题2:反复请求网络图片,增加用 ...
- iOS开发Swift篇—(二)变量和常量
iOS开发Swift篇—(二)变量和常量 一.语言的性能 (1)根据WWDC的展示 在进行复杂对象排序时Objective-C的性能是Python的2.8倍,Swift的性能是Python的3.9倍 ...
- iOS开发CoreAnimation解读之二——对CALayer的分析
iOS开发CoreAnimation解读之二——对CALayer的分析 一.UIView中的CALayer属性 1.Layer专门负责view的视图渲染 2.自定义view默认layer属性的类 二. ...
- iOS 11开发教程(二十二)iOS11应用视图实现按钮的响应(2)
iOS 11开发教程(二十二)iOS11应用视图实现按钮的响应(2) 此时,当用户轻拍按钮后,一个叫tapButton()的方法就会被触发. 注意:以上这一种方式是动作声明和关联一起进行的,还有一种先 ...
- iOS 11开发教程(二十一)iOS11应用视图美化按钮之实现按钮的响应(1)
iOS 11开发教程(二十一)iOS11应用视图美化按钮之实现按钮的响应(1) 按钮主要是实现用户交互的,即实现响应.按钮实现响应的方式可以根据添加按钮的不同分为两种:一种是编辑界面添加按钮实现的响应 ...
- iOS 11开发教程(二十)iOS11应用视图美化按钮之设置按钮的状态
iOS 11开发教程(二十)iOS11应用视图美化按钮之设置按钮的状态 在示例2-2中,设置按钮的标题和颜色时,需要对按钮的状态进行设置,表示按钮在某一状态下的标题和标题颜色是什么样子.例如,UICo ...
- iOS 11开发教程(二)编写第一个iOS 11应用
iOS 11开发教程(二)编写第一个iOS 11应用 编写第一个iOS 11应用 本节将以一个iOS 11应用程序为例,为开发者讲解如何使用Xcode 9.0去创建项目,以及iOS模拟器的一些功能.编 ...
- Quartz 2D在ios中的使用简述二:创建画布
在iOS中使用Quartz画图时,第一步就是要获取画布(图形上下文),然后再画布上做各种操作.先看下CoreGraphics.h这个头文件,就可以知道能够创建多少种上下文类型. #include &l ...
- iOS之2016面试题二
前言 招聘高峰期来了,大家都非常积极地准备着跳槽,那么去一家公司面试就会有一堆新鲜的问题,可能不会,也可能会,但是了解不够深.本篇文章为群里的小伙伴们去要出发公司的笔试题,由笔者整理并提供笔者个人参考 ...
随机推荐
- SQL Server实现跨库查询(跨库select insert)
方法一: select * from servername.dbo.tablename 方法二: select * from OPENDATASOURCE( 'SQLOLEDB', ...
- JS对象 神奇的Math对象,提供对数据的数学计算。注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别
Math对象 Math对象,提供对数据的数学计算. 使用 Math 的属性和方法,代码如下: <script type="text/javascript"> var m ...
- 随笔记录 linux命令 2019.7.29
系统命令 一. type 查看命令是内部命令还是内部命令 help 帮助 man 在线帮助 cd 切换目录 pwd 查看所在路径 stat 查看文件详细信息 ls ...
- log-slave-updates参数
从库做为其他从库的主库时 log-slave-updates参数是必须要添加的,因为从库要作为其他从库的主库,必须添加该参数.该参数就是为了让从库从主库复制数据时可以写入到binlog日志,为什么要用 ...
- Android开发 QRCode二维码开发第三方框架
前言 Android开发里二维码开发经常用到,这里简单的介绍下Android开发里的二维码. 最广泛使用的二维码库zxing zxing是最广泛的二维码库各个平台都可以适用它,但是Android平台使 ...
- Controller 获取前端数据
默认支持的类型 在controller的方法的形参中直接定义上面这些类型的参数,springmvc会自动绑定. HttpServletRequest对象 HttpServletResponse对象 H ...
- 专访阿里云MVP黄胜蓝:90 后 CTO花了6年,改变了你日常生活里的这件事
[黄胜蓝:现任武汉极意网络科技有限公司CTO.高中时期NOIP一等奖保送至武汉大学,大学期间曾指导团队获得世界数学建模大赛金奖,同时负责武汉大学学生校园门户网站的运维工作.于2013年加入武汉极意网络 ...
- 线段树+欧拉函数——cf1114F
调了半天,写线段树老是写炸 /* 两个操作 1.区间乘法 2.区间乘积询问欧拉函数 欧拉函数计算公式 phi(mul(ai))=mul(ai) * (p1-1)/p1 * (p2-1)/p2 * .. ...
- Vue-Grid-Layout分享一款好用的可拖拽组件
在使用Grafana的过程中,发现Grafana关于视图页面中每一个面板都可拖拽,可随意放大放小,体验非常棒,F12看了Grafana的代码,看打包后的代码很像react,进一步css,看到有grid ...
- 更改网卡名称以及重启网卡提示Determining if ip address x.x.x.x is already in use for device eth0
安装系统完成后,在CentOS6.6下网卡名称变为em1,有些不太方便,还是改回eth0 修改grub配置文件,vi /boot/grub/grub.conf,增加如下红色字体 kernel /vml ...