UIBezierPath 的理解 及使用

UIBezierPath这个类呢主要用于绘图。

使用

UIBezierPath的使用相当简单,分为三步: * 创建path * 添加路径到path * 将path绘制出来 例如我们来画条线: “

objc // 创建path UIBezierPath *path = [UIBezierPath bezierPath];

// 添加路径[1条点(100,100)到点(200,100)的线段]到path[path moveToPoint:CGPointMake(100 , 100)];[path addLineToPoint:CGPointMake(200, 100)];// 将path绘制出来[path stroke];

同样的我们也可以画一个圆 `objc UIBezierPath *path = [UIBezierPath bezierPath]; [path addArcWithCenter:self.center radius:100.0 startAngle:0.0 endAngle:180.0 clockwise:YES]; [path stroke];`

除了画线、画圆之外,我们还可以画其他各种图形。 具体的方法你都可以在UIBezierPath的头文件里面找到,而使用过程和Core Graphics基本一致。

其中有一个地方是需要我们注意的。就是颜色的设置。

UIBezierPath颜色的设置并没有包含在自己类中,而是通过UIColor直接设置的。 例:

// 设置描边色[[UIColor blueColor] setStroke];// 设置填充色[[UIColor redColor] setFill];看上去是UIColor的方法,其实也是对于CGContextRef的渲染,最终还是作用到CGConextRef上的 而UIBezierPath其实也就是对CGPathRef的封装所以UIBezierPath通过UIColor的方法来设置颜色也就不奇怪了。因为UIColor和UIBezierPath最终还是通过Core Graphics的方法来绘图的,只不过苹果又封装了一层OC。最后结合上面那个画圆的例子,把颜色添加进去,再来看下效果。//创建pathUIBezierPath *path = [UIBezierPath bezierPath];// 添加圆到path[path addArcWithCenter:self.center radius:100.0 startAngle:0.0 endAngle:180.0 clockwise:YES];// 设置描边宽度(为了让描边看上去更清楚)[path setLineWidth:5.0];//设置颜色(颜色设置也可以放在最上面,只要在绘制前都可以)[[UIColor blueColor] setStroke];[[UIColor redColor] setFill];// 描边和填充[path stroke];[path fill];

UIBezierPath类提供了添加二次贝塞尔曲线和三次贝塞尔曲线的支持。 曲线段在当前点开始,在指定的点结束。曲线的形状有开始点,结束点,一个或者多个控制点的切线定义。下图显示了两种曲线类型的相似,以及控制点和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]; }

绘制三次贝塞尔曲线

– (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]; }

使用Core Graphics函数去修改path UIBezierPath类只是CGPathRef数据类型和path绘图属性的一个封装。虽然通常我们可以用UIBezierPath类的方法去添加直线段和曲线段,UIBezierPath类还提供了一个属性CGPath,我们可以用来直接修改底层的path data type。如果我们希望用Core Graphics 框架函数去创建path,,则我们要用到此属性。

有两种方法可以用来修改和UIBezierPath对象相关的path。可以完全的使用Core Graphics函数去修改path,也可以使用Core Graphics函数和UIBezierPath函数混合去修改。第一种方法在某些方面相对来说比较容易。我们可以创建一个CGPathRef数据类型,并调用我们需要修改path信息的函数。

下面的代码就是赋值一个新的CGPathRef给UIBezierPath对象。

// Create the path dataCGMutablePathRef cgPath = CGPathCreateMutable();CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(0, 0, 300, 300));CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(50, 50, 200, 200));// Now create the UIBezierPath objectUIBezierPath* 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);而是深沉的意志恢弘的想象炙热的恋情;青春是生命的深泉在涌流。

UIBezierPath 的理解 及使用

相关文章:

你感兴趣的文章:

标签云: