全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

快速上手IOS UIBezierPath(贝塞尔曲线)

UIBezierPath主要用来绘制矢量图形,它是基于Core Graphics对CGPathRef数据类型和path绘图属性的一个封装,所以是需要图形上下文的(CGContextRef),所以一般UIBezierPath在drawRect中使用。

使用方法

UIBezierPath 是对 CGPathRef 的封装。创建矢量图形时,拆解成一或多条线段,拼接起来,每条线段的终点都是下一条线段的起点。

具体地:

1.创建一个 UIBezierPath 对象

2.用 moveToPoint: 设置初始线段的起点

3.添加线段,定义一或多个子路径

4.修改 UIBezierPath 的绘图相关的属性,比如stroke path的属性 lineWidth 和 lineJoinStyle ,filled path的属性 usesEvenOddFillRule

注意:如果是矩形或者圆之类的特殊图形,可以不用第2步。

代码案例

画直线

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)];
path.lineWidth = 5.0f;
path.lineJoinStyle = kCGLineJoinRound;
[path stroke];

创建三角形

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(300, 50)];
[path addLineToPoint:CGPointMake(200, 150)];
// 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加
// [path addLineToPoint:CGPointMake(50, 50)];
[path closePath];
path.lineWidth = 5.0f;
[path stroke];

创建矩形

UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 100, 50, 50)];
path.lineWidth = 5.0f;
[path stroke];

创建内切曲线

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 50, 50)];
path.lineWidth = 5.0f;
[path stroke];

 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 50, 100)];
 path.lineWidth = 5.0f;
 [path stroke];

创建带有圆角的矩形,当矩形变成正圆的时候,Radius就不再起作用

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 300, 50, 50) cornerRadius:15.0f];
path.lineWidth = 5.0f;
[path stroke];

设定特定的角为圆角的矩形

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 400, 50, 50) byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5,5)];
path.lineWidth = 5.0f;
[path stroke];

创建圆弧

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 550) radius:25 startAngle:0 endAngle:1.5*M_PI clockwise:YES];
path.lineWidth = 5.0f;
[path stroke];

通过路径A创建路径B

UIBezierPath *path_A = [UIBezierPath bezierPath];
[path_A moveToPoint:CGPointMake(200, 50)];
[path_A addLineToPoint:CGPointMake(250, 100)];
path_A.lineWidth = 5.0f;
UIBezierPath *path_B = [UIBezierPath bezierPathWithCGPath:path_A.CGPath];
[path_B stroke];

创建三次贝塞尔曲线

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 200)];
[path addCurveToPoint:CGPointMake(300, 200) controlPoint1:CGPointMake(150, 150) controlPoint2:CGPointMake(250, 250)];
[path stroke];

创建二次贝塞尔曲线

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 200)];
[path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(150, 150)];
[path stroke];

添加圆弧

 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(200, 400)];
 [path addLineToPoint:CGPointMake(225, 410)];
 [path addArcWithCenter:CGPointMake(200, 400) radius:25 startAngle:0 endAngle:1.5*M_PI clockwise:YES];
// [path closePath];
// [path removeAllPoints];
 [path stroke];

追加路径

UIBezierPath *path_A = [UIBezierPath bezierPath];
[path_A moveToPoint:CGPointMake(200, 500)];
[path_A addLineToPoint:CGPointMake(225, 410)];
UIBezierPath *path_B = [UIBezierPath bezierPath];
[path_B moveToPoint:CGPointMake(200, 600)];
[path_B addLineToPoint:CGPointMake(225, 500)];
[path_A appendPath:path_B];
[path_A stroke];

创建翻转路径,即起点变成终点,终点变成起点

 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(50, 50)];
 [path addLineToPoint:CGPointMake(100, 50)];
 path.lineWidth = 5.0f;
 NSLog(@"%@",NSStringFromCGPoint(path.currentPoint));
 UIBezierPath *path_b = [path bezierPathByReversingPath];
 CGAffineTransform transform = CGAffineTransformMakeTranslation(200, 0);
 [path_b applyTransform: transform];
 // 两条路径分别添加一条直接到 self.center
 [path addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)];
 [path_b addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)];
 NSLog(@"%@",NSStringFromCGPoint(path_b.currentPoint));
 [[UIColor redColor] set];
 [path stroke];
 [[UIColor blueColor] set];
 [path_b stroke];

路径进行仿射变换

 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(100, 50)];
 [path addLineToPoint:CGPointMake(200, 50)];
 CGAffineTransform transform = CGAffineTransformRotate(self.transform, M_PI_4);
 [path applyTransform:transform];
 path.lineWidth = 5.0f;
 [path stroke];

创建虚线

CGFloat dashStyle[] = {1.0f, 2.0f};
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)];
[path setLineDash:dashStyle count:2 phase:0.0];
[path stroke];

设置颜色

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];
[[UIColor greenColor] setStroke];
[[UIColor redColor] setFill];
[path stroke];
[path fill];

设置描边混合模式

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];
[[UIColor greenColor] setStroke];
path.lineWidth = 10.0f;
[path strokeWithBlendMode:kCGBlendModeSaturation alpha:1.0];
[path stroke];
 

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];
 [[UIColor redColor] setFill];
 [path fillWithBlendMode:kCGBlendModeSaturation alpha:0.6];
 [path fill];

修改当前图形上下文的绘图区域可见,随后的绘图操作导致呈现内容只有发生在指定路径的填充区域

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];
[[UIColor greenColor] setStroke];
[path addClip];
[path stroke];

结语

关于UIBezierPath的简单介绍就到这了,主要是用代码做了展示,属性跟方法,没详细去介绍,我觉得可以直接看苹果的api写的也蛮清楚的.或者自己试试不同的参数样式也能大概理解了.

核心动画跟贝赛尔曲线都有了简单的介绍了,接下来就可以动手做点简单的自定义动画了.

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


# uibezierpath  # ios  # 贝塞尔曲线  # 贝塞尔曲线案例  # ios 贝塞尔曲线切割圆角的方法  # iOS贝塞尔曲线画哆啦A梦的代码实例  # IOS 贝塞尔曲线(UIBezierPath)属性、方法整理  # iOS实现贝塞尔曲线动画  # 方法来  # 塞尔  # 都是  # 圆角  # 角形  # 我觉得  # 就不  # 也能  # 它是  # 可以通过  # 可以直接  # 自定义  # 两条  # 就到  # 每条  # 创建一个  # 多条  # 自动生成  # 画了  # 做点 


相关文章: 如何在Golang中处理模块冲突_解决依赖版本不兼容问题  如何高效配置IIS服务器搭建网站?  家庭建站与云服务器建站,如何选择更优?  广德云建站网站建设方案与建站流程优化指南  平台云上自助建站如何快速打造专业网站?  高性价比服务器租赁——企业级配置与24小时运维服务  高端智能建站公司优选:品牌定制与SEO优化一站式服务  c++23 std::expected怎么用 c++优雅处理函数错误返回【详解】  如何将凡科建站内容保存为本地文件?  网站制作软件免费下载安装,有哪些免费下载的软件网站?  如何用低价快速搭建高质量网站?  如何选择高效稳定的ISP建站解决方案?  网站制作公司排行榜,抖音怎样做个人官方网站  如何使用Golang table-driven基准测试_多组数据测量函数效率  交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?  已有域名如何快速搭建专属网站?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  建站OpenVZ教程与优化策略:配置指南与性能提升  小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建  如何快速登录WAP自助建站平台?  如何选择高效便捷的WAP商城建站系统?  长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?  如何在万网开始建站?分步指南解析  常州自助建站工具推荐:低成本搭建与模板选择技巧  c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】  如何在Golang中指定模块版本_使用go.mod控制版本号  黑客如何利用漏洞与弱口令入侵网站服务器?  上海制作企业网站有哪些,上海有哪些网站可以让企业免费发布招聘信息?  建站三合一如何选?哪家性价比更高?  北京营销型网站制作公司,可以用python做一个营销推广网站吗?  如何用花生壳三步快速搭建专属网站?  代购小票制作网站有哪些,购物小票的简要说明?  简易网站制作视频教程,使用记事本编写一个简单的网页html文件?  图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?  如何配置FTP站点权限与安全设置?  小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化  详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)  在线教育网站制作平台,山西立德教育官网?  道歉网站制作流程,世纪佳缘致歉小吴事件,相亲网站身份信息伪造该如何稽查?  如何在万网自助建站中设置域名及备案?  建站之星Pro快速搭建教程:模板选择与功能配置指南  如何快速生成可下载的建站源码工具?  ,石家庄四十八中学官网?  seo网站制作优化,网站SEO优化步骤有哪些?  建站主机选择指南:服务器配置与SEO优化实战技巧  如何快速选择适合个人网站的云服务器配置?  如何在阿里云ECS服务器部署织梦CMS网站?  如何使用Golang安装API文档生成工具_快速生成接口文档  网站制作网站,深圳做网站哪家比较好?  如何通过.red域名打造高辨识度品牌网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。