iOS开发 贝塞尔曲线UIBezierPath
最近项目中需要用到用贝塞尔曲线去绘制路径 ,然后往路径里面填充图片,找到这篇文章挺好,记录下来 自己学习! 转至 http://blog.csdn.net/guo_hongjun1611/article/details/7839371
使用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); |
} |
iOS开发 贝塞尔曲线UIBezierPath的更多相关文章
- iOS开发 贝塞尔曲线UIBezierPath(2)
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- iOS开发 贝塞尔曲线UIBezierPath(后记)
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- iOS开发 贝塞尔曲线
iOS开发 贝塞尔曲线UIBezierPath - 陌云 时间 2014-03-14 11:04:00 博客园-所有随笔区 原文 http://www.cnblogs.com/moyunmo/p/ ...
- 贝塞尔曲线UIBezierPath简单使用
//常用属性 /* 1.CGPath: 将UIBezierPath类转换成CGPath 2.currentPoint: 当前path的位置,可以理解为path的终点 3.lineWidth: 线条宽度 ...
- iOS 使用贝塞尔曲线绘制路径
使用贝塞尔曲线绘制路径 大多数时候,我们在开发中使用的控件的边框是矩形,或者做一点圆角,是使得矩形的角看起来更加的圆滑. 但是如果我们想要一个不规则的图形怎么办?有人说,叫UI妹子做,不仅省事,还可以 ...
- iOS:使用贝塞尔曲线绘制图表(折线图、柱状图、饼状图)
1.介绍: UIBezierPath :画贝塞尔曲线的path类 UIBezierPath定义 : 贝赛尔曲线的每一个顶点都有两个控制点,用于控制在该顶点两侧的曲线的弧度. 曲线的定义有四个点:起始点 ...
- iOS贝塞尔曲线(UIBezierPath)的基本使用方法
简介 UIBezierPath是对Core Graphics框架的一个封装,使用UIBezierPath类我们可以画出圆形(弧线)或者多边形(比如:矩形)等形状,所以在画复杂图形的时候会经常用到. 分 ...
- 通过UIBezierPath贝塞尔曲线画圆形、椭圆、矩形
/**创建椭圆形的贝塞尔曲线*/ UIBezierPath *_ovalPath=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )]; ...
- iOS关于CAShapeLayer与UIBezierPath的知识内容
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
随机推荐
- cocos2d-x mac os启动 android helloworld
工具环境: mac os, jdk, Android Studio, cocos2d-x-3.13.1, ant, android-ndk. 解压coco2d-x后,根目录下有 setup.py, 命 ...
- java 截取pdf
最近在读一本电子书,pdf中频繁引用后文的内容(页码),必须实时的跳过去看,但是扫描版的pdf的页码往往从封面就开始计数,而且盗版还经常有一些做广告的页面,这就导致pdf reader 索引的页面并不 ...
- synchronized同步块和volatile同步变量
Java语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量.这两种机制的提出都是为了实现代码线程的安全性.其中 Volatile 变量的同步性较差(但有时它更简单并且开销更低),而 ...
- 对于字符串拼接,string.format、stringbuilder、+=
sring拼接经常会用到,拼接时候使用的方法,每个人的又不一样,有的是不知道哪个效率高,也有一些是为了方便不差那么一点时间! 今天百度查了查他们的区别! += 是效率最低的一个,尽量避免使用,当然,不 ...
- ubuntu15.04安装Chrome浏览器
首先到: https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 下载最新的安装文件. 然后: sudo a ...
- Android -- 使用ViewPager实现画廊效果
1,今天在微信推送文章看到实现画廊效果,感觉挺不错的,就来写写试试,先来看一下效果图: 上面的效果基本上可以用两个功能点来包含:ViewPager的切换动画.ImageView的倒影的实现 嗯,先来将 ...
- asp检测数字类型函数
'**************************************************'函数ID:0014[检测ID是否为数字类型]'函数名:JCID'作 用:检测ID是否为数字类型' ...
- jQuery与其他JS库冲突解决
实际开发中遇到JQuery与其他js库起冲突 究其原因,是它们的全局对象定义冲突了,特别是变量”$”, 可重载$函数.使用jQuery.noConflict()就可以通过重载$函数 例:项目中应用的 ...
- 机器学习实战-边学边读python代码(4)
程序2-4 分类器针对约会网站的测试代码(4) def datingClassTest():hoRatio = 0.10 //将文件读入内存矩阵datingDataMat,datingLabels = ...
- javadoc 生成文档注释
我们可以通过 javadoc 命令从文档注释中提取内容,生成程序的 API 帮助文档. javadoc -d doc demo.java 文档注释:/******/ @author 标明开发该类模块的 ...