iOS UIBezierPath简单实用

摘要:
设置初始点4。使用UIBezierPath类方法根据特定要求进行绘制(例如,绘制直线、矩形、圆、圆弧等)5。设置与UIBezierPath对象相关的属性(例如,lineWidth、lineJoinStyle、aPath.lineCapStyle、颜色)6。在介绍其他使用方法之前,请使用笔划或填充方法结束图形:

 转载请注明出处!!!

1.介绍 

     UIBezierPath这个类在UIKit中, 是Core Graphics框架关于path的一个封装,使用此类可以定义简单的形状,比如我们常用到,矩形,圆形,椭圆,弧,或者不规则的多边形

     每一个直线段或者曲线段的结束的地方是下一个的开始的地方。每一个连接的直线或者曲线段的集合成为subpath。一个UIBezierPath对象定义一个完整的路径包括一个或者多个subpaths。

     我们一般使用UIBezierPath都是在重写view的drawRect方法这种情形。我们用直线去创建矩形或多边形,使用曲线创建弧或者圆。

     使用步骤:

     1、 重写View的drawRect方法

     2、 创建UIBezierPath的对象

     3、 使用方法moveToPoint: 设置初始点

     4、 根据具体要求使用UIBezierPath类方法绘图(比如要画线、矩形、圆、弧?等)

     5、 设置UIBezierPath对象相关属性 (比如lineWidth、lineJoinStyle、aPath.lineCapStyle、color)

     6、 使用stroke 或者 fill方法结束绘图

在介绍其他使用方法之前,我们先来看一下 path 的几个属性,以便下面我进行设置。

     1、 [color set]; 设置线条颜色,也就是相当于画笔颜色

     2、 path.lineWidth = 5.0; 这个很好理解了,就是划线的宽度

     3、 path.lineCapStyle 这个线段起点是终点的样式,这个样式有三种:

        (

            1、 kCGLineCapButt 该属性值指定不绘制端点, 线条结尾处直接结束。这是默认值。

            2、 kCGLineCapRound 该属性值指定绘制圆形端点, 线条结尾处绘制一个直径为线条宽度的半圆。

            3、 kCGLineCapSquare 该属性值指定绘制方形端点。 线条结尾处绘制半个边长为线条宽度的正方形。需要说明的是,这种形状的端点与“butt”形状的端点十分相似,只是采用这种形式的端点的线条略长一点而已

        )

     4、 path.lineJoinStyle 这个属性是用来设置两条线连结点的样式,同样它也有三种样式供我们选择

        (

            1、 kCGLineJoinMiter 斜接

            2、 kCGLineJoinRound 圆滑衔接

            3、 kCGLineJoinBevel 斜角连接

        )

     5、 [path stroke]; 用 stroke 得到的是不被填充的 view , [path fill]; 用 fill 得到的内部被填充的 view,这点在下面的代码还有绘制得到的图片中有,可以体会一下这两者的不同。

2.一些简单使用代码

(1) 直线

- (void)drawView1 {
    UIColor *color = [UIColor redColor];
    [color set]; //设置线条颜色
    UIBezierPath *myPath = [UIBezierPath bezierPath];
    [myPath moveToPoint:CGPointMake(200, 50)];
    [myPath addLineToPoint:CGPointMake(300, 100)];
    myPath.lineWidth = 5.0;
    myPath.lineCapStyle = kCGLineCapRound; //线条拐角
    myPath.lineJoinStyle = kCGLineJoinRound; //终点处理
    [myPath stroke];
}

(2) 多边形

- (void)drawView2 {
    UIColor *color = [UIColor redColor];
    [color set]; //设置线条颜色
    
    UIBezierPath *myPath = [UIBezierPath bezierPath];
    [myPath moveToPoint:CGPointMake(200, 50)];
    [myPath addLineToPoint:CGPointMake(300, 100)];
    [myPath addLineToPoint:CGPointMake(260, 200)];
    [myPath addLineToPoint:CGPointMake(100, 200)];
    [myPath addLineToPoint:CGPointMake(100, 70)];
    [myPath closePath];

    myPath.lineWidth = 5.0;
    myPath.lineCapStyle = kCGLineCapRound; //线条拐角
    myPath.lineJoinStyle = kCGLineJoinRound; //终点处理
    
//    [myPath fill];//颜色填充
    [myPath stroke]; //内部无色

    //closePath方法不仅结束一个shape的subpath表述,它也在最后一个点和第一个点之间画一条线段,这个一个便利的方法我们不需要去画最后一条线了

}

(3) 矩形正方形 和一些特殊

- (void)drawView3 {
    UIColor *color = [UIColor redColor];
    [color set];

//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)];
    // 带弧度的矩形
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 15, 100, 80) cornerRadius:10];
 
    // 部分圆角 
    UIBezierPath *path =[UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 45, 90, 90)
                                                byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft
                                                      cornerRadii:CGSizeMake(10, 10)];
    
    path.lineWidth = 5.0;
    path.lineCapStyle = kCGLineCapRound; //线条拐角
    path.lineJoinStyle = kCGLineJoinRound; //终点处理
    [path stroke];

}

(4) 椭圆 圆   + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect

- (void)drawView4 {
    UIColor *color = [UIColor redColor];
    [color set];

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 100, 80)];
    path.lineWidth = 5.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path stroke];
}

(5) 弧线   + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwis

/*

 ArcCenter: 原点

 radius: 半径

 startAngle: 开始角度

 endAngle: 结束角度  弧度制  X轴正方形为0 圆2*3.14

 clockwise: 是否顺时针方向 /

 */

- (void)drawView5 {
    UIColor *color = [UIColor redColor];
    [color set];

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:90 startAngle:0 endAngle:DEGREES_TO_RADIANS(90) clockwise:YES];
    
    path.lineWidth = 5.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    [path stroke];
}

(6) 二级贝塞尔曲线 - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint

// 这个方法绘制二次贝塞尔曲线。曲线段在当前点开始,在指定的点结束,
/*
 endpoint  终点  controlPoint 切点 两条曲线的切线的交点
 */

- (void)drawView6 {
    UIColor *color = [UIColor redColor];
    [color set];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    path.lineWidth = 5.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    
    [path moveToPoint:CGPointMake(40, 150)];
    [path addQuadCurveToPoint:CGPointMake(30, 400) controlPoint:CGPointMake(120, 40)];
    [path stroke];

}

(7)绘制三次贝塞尔曲线 - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2

// 这个方法绘制三次贝塞尔曲线。曲线段在当前点开始,在指定的点结束,两个控制点的切线定义

/*
 endPoint 终点 controlPoint1 与起点连线的线与弧线相切 controlPoint2 与终点连线的线与弧线相切
 */

- (void)drawView7 {
    UIColor *color = [UIColor redColor];
    [color set];

    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 5.0;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineJoinRound;
    
    [path moveToPoint:CGPointMake(40, 150)];
    [path addCurveToPoint:CGPointMake(260, 200)
            controlPoint1:CGPointMake(140, 0)
            controlPoint2:CGPointMake(140, 400)];;
    [path stroke];
}

 

免责声明:文章转载自《iOS UIBezierPath简单实用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux 查看 cpu个数 核心数 线程数rtsp学习----海康RTSP客户端连接深入分析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF 实现已绑定集合项目的移除时动画过渡

信不信由你,这个场景貌似没有官方的完美解决方案。我认为这个要求一点都不过分,花了很长时间 bai google du,就是没找到好的方案。这是我花了一天时间才搞通的一个方案,跟大家分享,虽然仍然不太完美,但是希望对大家有用。 对完美的憧憬 一个已绑定到 ItemsControl 的集合,是不能通过 ItemsControl.Items 属性来访问的,如果你...

高德地图API之公交路线

引入插件 AMap.Transfer <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>map</title> <script type="text/javascri...

JVM知识整理和学习(转载并修改)

  JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构的设计原理。   冯·诺依曼体系结构中,指出计算机处理的数据和指令都是二进制数,采用存储程序方式不加区分的存储在同一个存储器里,并且顺序执行,指令由操作码和地址码组成,操作码决定了操作类型和所操作的数的数字类型,地址码则指出地址码和操作数。   从dos到window8,从unix到ubuntu和...

android开发之shape详解

很多时候,使用shape能够实现的效果,你用一张图片也能够实现,但问题是一张图片无论你怎么压缩,它都不可能比一个xml文件小,因此,为了获得一个高性能的手机App,我们在开发中应该遵循这样一个原则:能够用shape实现的效果尽量不使用图片来实现。 今天我们就一起来看看shape的使用。 首先,使用shape画的图形,这个xml文件的根节点是shape,如下...

JQuery表格操作的常用技巧总结

JQuery对表格进行操作的常用技巧。 1、表格奇数行和偶数行分别添加样式  复制代码代码如下: $(function(){  $('tr:odd').addClass("odd");  $('tr:even').addClass("even");  });  不算表的头部  复制代码代码如下: $(function(){  $('tbody>...

Android Studio 40个快捷键

1、Alt+ F8:计算值。 2、Ctrl+F7:快速查找。 3、Ctrl+H:查看继承关系。 4、Ctrl+P:提示有效说明参数。 5、Ctrl+Alt+V:提取变量。 6、Shift+F6:重命名。 7、Shift+左键:关闭标签。 8、Ctrl+D: 集合了复制和粘贴两个操作,如果有选中的部分就复制选中的部分,并在选中部分的后面粘贴出来,如果没有选中...