iOS之CGPath的应用(二)
2024-08-25 22:22:27
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];
}
效果图
最新文章
- Linux0.11内核--内存管理之2.配合fork
- Gephi可视化(二)——Gephi Toolkit叫板Prefuse
- Linux版MonoDevelop无法连接调试器的解决方案(Could not connet to the debugger)
- YSLOW
- 从RTSP协议SDP数据中获得二进制的SPS、PPS
- Toolbar的使用
- 【转】cloudera新增用户权限配置
- 将真彩色转换成增强色的方法(即RGB32位或RGB24位颜色转换成RGB16位颜色的函数)
- Java 关键字final
- 爱重启的windows,伤不起
- Windows Auzre 微软的云计算产品的后台操作界面
- Fragment与Activity交互(使用Bundle)
- 打开地图文件和shape文件代码加载Mxd文档
- JVM、GC与HashMap
- OpenSSL中的大数接口与基于其的自用RSA加密接口设计
- Google word/sheets 常见的使用:
- 爱奇艺2017秋招笔试(C++智能设备方向)
- c#反射(1)
- ACM-ICPC 2018 南京赛区网络预赛 B The writing on the wall(思维)
- Bootstrap3的输入框数字点击修改效果
热门文章
- USACO 2011 November Cow Lineup /// map set 尺取法 oj25279
- 增量+全量备份SVN服务器
- Shell 学习(三)
- 2019-4-29-WPF-如何判断一个控件在滚动条的里面是用户可见
- python2x 安装 psutil
- AM8后台历史数据手工清理方法
- day20 装饰器补充
- System.Web.Mvc.HttpGetAttribute.cs
- R软件导入数据_r语言怎么导入数据_R软件导入数据
- day 68 Django基础四之模板系统