Objective-C用UIBezierPath画图
来源:互联网 发布:杭州月嫂哪家好 知乎 编辑:程序博客网 时间:2024/06/14 17:13
UIBezierPath是Core Graphics框架关于路径的封装。我们通过设置路径即可画出我们想要的一些简单图形。如一个简单的需求:IM消息的展示框的一个“尖”:
还有下面这个:
这样一个不规则的图形我们只需要在规则的矩形View右边紧挨着画一个小三角即可。下面的代码使用UIBezierPath画了一个三角形:
- (void)drawTrianglePath { //初始化 UIBezierPath *path = [UIBezierPath bezierPath]; //初始点 [path moveToPoint:CGPointMake(20, 20)]; //第二点 [path addLineToPoint:CGPointMake(self.frame.size.width - 40, 20)]; //第三个点 [path addLineToPoint:CGPointMake(self.frame.size.width / 2, self.frame.size.height - 20)]; // 最后的闭合线是可以通过调用closePath方法来自动生成的,也可以调用-addLineToPoint:方法来添加 // [path addLineToPoint:CGPointMake(20, 20)]; [path closePath]; // 设置线宽 path.lineWidth = 1.5; // 设置填充颜色 UIColor *fillColor = [UIColor greenColor]; [fillColor set]; [path fill]; // 设置画笔颜色 UIColor *strokeColor = [UIColor blueColor]; [strokeColor set]; // 根据我们设置的各个点连线 [path stroke];}
我们同样可以多设置一些点来绘制其他的图形,可以使用+ bezierPathWithOvalInRect:方法来画圆,当我们传的rect参数是一个正方形时,画出来的就是圆(矩形则是椭圆):
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, self.frame.size.width - 40, self.frame.size.width - 40)];
使用UIBezierPath画的图需要放到view的drawRect方法中才能显示 ,如果想要在其他方法中显示需要借助CAShapeLayer类。把UIBezierPath类的path属性赋值给CAShapeLayer的path,然后将CAShapeLayer对象添加到对应View的layer上面即可。
这里有一篇博客介绍UIBezierPath的使用,很详细:http://www.huangyibiao.com/archives/190
0 0
- Objective-C用UIBezierPath画图
- UIBezierPath画图
- Objective-C 画图
- ios-画图UIBezierPath
- 学习UIBezierPath画图
- iOS CAShapeLayer & UIBezierPath画线、画图
- 用C语言画图
- iOS画图-UIBezierPath和CAShapeLayer的简单使用
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- UIBezierPath
- 1.two sum
- Java与算法之(4) - 数字全排列
- BGRABitmap图像操作3:用鼠标在窗体画断续的曲线
- mysql中update和delete时sql_safe_updates问题
- MVC显示表格,点击列头可以排序
- Objective-C用UIBezierPath画图
- leetcode:Trie:Add and Search Word - Data structure design(211)
- C++ 虚函数表解析
- 字符识别算法SWT实现中的一些经验总结
- 驰骋工作流引擎表单设计控件-关系类控件-明细表(3)
- 72. Edit Distance 最小编辑距离
- R树空间索引
- 项目管理之如何控制项目进度和质量
- android6.0 Scrollview嵌套RecyclerView position始终为0