UIBezierPath 画线
使用UIBezierPath类可以创建基于矢量的路径,这个类在UIKit中。此类是Core Graphics框架关于path的一个封装。使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状。
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- UIBezierPath* aPath = [UIBezierPath bezierPath];
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- // Set the starting point of the shape.
- [aPath moveToPoint:CGPointMake(100.0, 0.0)];
- // Draw the lines
- [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
- [aPath addLineToPoint:CGPointMake(160, 140)];
- [aPath addLineToPoint:CGPointMake(40.0, 140)];
- [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
- [aPath closePath];//第五条线通过调用closePath方法得到的
- [aPath stroke];//Draws line 根据坐标点连线
- }
注:这个类要继承自UIView。
- Creates and returns a new UIBezierPath object initialized with a rectangular path.
- + (UIBezierPath *)bezierPathWithRect:(CGRect)rect
demo代码:
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 50)];
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- [aPath stroke];
- }
4、使用UIBezierPath创建圆形或者椭圆形
- Creates and returns a new UIBezierPath object initialized with an oval path inscribed in the specified rectangle
- + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
这个方法根据传入的rect矩形参数绘制一个内切曲线。
- Creates and returns a new UIBezierPath object initialized with an arc of a circle.
- + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
- Parameters
- center
- Specifies the center point of the circle (in the current coordinate system) used to define the arc.
- radius
- Specifies the radius of the circle used to define the arc.
- startAngle
- Specifies the starting angle of the arc (measured in radians).
- endAngle
- Specifies the end angle of the arc (measured in radians).
- clockwise
- The direction in which to draw the arc.
- Return Value
- A new path object with the specified arc.
- #define pi 3.14159265359
- #define DEGREES_TO_RADIANS(degrees) ((pi * degrees)/ 180)
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
- radius:75
- startAngle:0
- endAngle:DEGREES_TO_RADIANS(135)
- clockwise:YES];
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- [aPath stroke];
- }
结果如下图:
- Appends a quadratic Bézier curve to the receiver’s path.
- - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
- Parameters
- endPoint
- The end point of the curve.
- controlPoint
- The control point of the curve.
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- UIBezierPath* aPath = [UIBezierPath bezierPath];
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- [aPath moveToPoint:CGPointMake(20, 100)];
- [aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 0)];
- [aPath stroke];
- }
- Appends a cubic Bézier curve to the receiver’s path.
- - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
- Parameters
- endPoint
- The end point of the curve.
- controlPoint1
- The first control point to use when computing the curve.
- controlPoint2
- The second control point to use when computing the curve.
- - (void)drawRect:(CGRect)rect
- {
- UIColor *color = [UIColor redColor];
- [color set]; //设置线条颜色
- UIBezierPath* aPath = [UIBezierPath bezierPath];
- aPath.lineWidth = 5.0;
- aPath.lineCapStyle = kCGLineCapRound; //线条拐角
- aPath.lineJoinStyle = kCGLineCapRound; //终点处理
- [aPath moveToPoint:CGPointMake(20, 50)];
- [aPath addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100)];
- [aPath stroke];
- }
// Create the path data |
CGMutablePathRef cgPath = CGPathCreateMutable(); |
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(0, 0, 300, 300)); |
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(50, 50, 200, 200)); |
// Now create the UIBezierPath object |
UIBezierPath* aPath = [UIBezierPath bezierPath]; |
aPath.CGPath = cgPath; |
aPath.usesEvenOddFillRule = YES; |
// After assigning it to the UIBezierPath object, you can release |
// your CGPathRef data type safely. |
CGPathRelease(cgPath); |
Mixing Core Graphics and UIBezierPath
calls
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 300, 300)]; |
// Get the CGPathRef and create a mutable version. |
CGPathRef cgPath = aPath.CGPath; |
CGMutablePathRef mutablePath = CGPathCreateMutableCopy(cgPath); |
// Modify the path and assign it back to the UIBezierPath object |
CGPathAddEllipseInRect(mutablePath, NULL, CGRectMake(50, 50, 200, 200)); |
aPath.CGPath = mutablePath; |
// Release both the mutable copy of the path. |
CGPathRelease(mutablePath); |
Drawing a path in a view
- (void)drawRect:(CGRect)rect |
{ |
// Create an oval shape to draw. |
UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect: |
CGRectMake(0, 0, 200, 100)]; |
// Set the render colors |
[[UIColor blackColor] setStroke]; |
[[UIColor redColor] setFill]; |
CGContextRef aRef = UIGraphicsGetCurrentContext(); |
// If you have content to draw after the shape, |
// save the current state before changing the transform |
//CGContextSaveGState(aRef); |
// Adjust the view's origin temporarily. The oval is |
// now drawn relative to the new origin point. |
CGContextTranslateCTM(aRef, 50, 50); |
// Adjust the drawing options as needed. |
aPath.lineWidth = 5; |
// Fill the path before stroking it so that the fill |
// color does not obscure the stroked line. |
[aPath fill]; |
[aPath stroke]; |
// Restore the graphics state before drawing any other content. |
//CGContextRestoreGState(aRef); |
} |
UIBezierPath 画线的更多相关文章
- iOS小画板画线总结
一:基本画线: 使用贝赛尔曲线画: //创建路径 UIBezierPath* aPath = [UIBezierPath bezierPath]; //设置线宽 aPath.lineWidth = 5 ...
- MFC画线功能总结
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...
- MFC消息映射机制以及画线功能实现
---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...
- CGContextRef 画线简单用法
CGContextRef CGContextMoveToPoint(context,150,50);//圆弧的起始点 CGContextAddArcToPoint(context,100,80,130 ...
- Android中Path类的lineTo方法和quadTo方法画线的区别
转载:http://blog.csdn.net/stevenhu_223/article/details/9229337 当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的li ...
- C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9
视频图像处理系列 索引 VS2013下测试通过. 在百度中搜索关键字“DirectX SDk”,或者进入微软官网https://www.microsoft.com/en-us/download/det ...
- [修复] Firemonkey 画线问题(Android & iOS 平台)
问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing proble ...
- WPF画线问题,几千条以后就有明显的延迟了。
我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() { _path.Data = ...
- ios cocos2d 画线出现闪烁问题
根据http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/ 用cocos2d ...
随机推荐
- BZOJ3734 : [Ontak2013]Miny
将所有炸弹按坐标排序 x<-y连边表示x爆炸了y也会爆炸 如果是DAG则直接拓扑排序+DP求出每个点出发能走到的最左端和最右端的点 有环则SCC缩点后再拓扑 用线段树优化建图的过程 边数$O(n ...
- 【BZOJ】1066: [SCOI2007]蜥蜴(最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 本题想一想应该懂了的. 我们想啊,,每个点都有限制,每个点都可以跳到另一个有限制的点,每个有蜥 ...
- 【BZOJ】1085: [SCOI2005]骑士精神(A*启发式搜索)
http://www.lydsy.com/JudgeOnline/problem.php?id=1085 囧啊囧,看了题解后写了个程序,但是样例总过不了T+T,调试了不下于1个小时,肉眼对拍看了根本看 ...
- shell 之for [转]
本文也即<Learning the bash Shell>3rd Edition的第五章Flow Control之读书笔记之二,但我们将不限于此.flow control是任何编程语言中很 ...
- 使用RBTool自动提交code review请求
使用RBTool自动提交code review请求 前言 让我们回想一下手工提交review请求的过程: 首先得用 svn diff > filename.diff 生成diff文件. 然后输入 ...
- 你们以为运营商只是HTTP插点广告而已么?
国内某邮件服务商,近期在某南方地区有大量客户反应登录时出错和异常,于是工作人员进行了一下跟进,发现如下: 首先,邮件服务商登陆页面为普通HTTP协议发送,提交时通过JS进行RSA加密(没错,JS的RS ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5:compile
mvn clean mvn install mvn clean -Dmaven.test.skip=true install 出现上述问题,找不到很多类的一些方法. 解决方法: 1.Window -- ...
- PHP 文件系统管理函数与 preg_replace() 函数过滤代码
案例:在带行号的代码至文件 crop.js 中.用两种方法去掉代码前面的行号,带行号的代码片段: 1.$(function(){ 2. //初始化图片区域 3. var myimg = new Ima ...
- 一些App的User-Agent
天猫 Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; MI 2C Build/KTU84P) AppleWebKit/537.36 (KHTML, like ...
- 超实用PHP函数总结整理
超实用PHP函数总结整理 2014-12-06 分类:WEB开发.编程开发.首页精华暂无人评论 来源:月光光博客 分享到:更多8 1.PHP加密解密 PHP加密和解密函数可以用来加密一些 ...