UIBezierPath

作者: edison0428 | 来源:发表于2017-07-11 17:09 被阅读35次

    一个由直线和曲线线段组成的路径,可以在自定义视图中呈现,UIKit中的UIBezierPath是Core Graphics框架关于path的一个封装。可以创建基于矢量的路径,例如椭圆或者矩形,或者有多个直线和曲线段组成的形状

    • 创建一个UIBezierPath对象

    • bezierPath

    1.创建并返回一个新的UIBezierPath对象

    • bezierPathWithRect:

    1.以矩形路径初始化创建并返回一个新的UIBezierPath对象

       //画矩形
        UIBezierPath * recPath=[UIBezierPath bezierPathWithRect:CGRectMake(10, 50, 50, 50)];
        //线的宽度
        recPath.lineWidth=3;
        
        [recPath fill];
    
    • bezierPathWithOvalInRect:

    1.以椭圆路径初始化创建并返回一个新的UIBezierPath对象

    UIBezierPath * ovalPath =[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 130, 80, 50)];
        [ovalPath stroke];
    
    • bezierPathWithRoundedRect:cornerRadius:

    1.以圆角矩形路径初始化创建并返回一个新的UIBezierPath对象

    UIBezierPath * recRoundPath=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 200, 80, 50) cornerRadius:10];
        [recRoundPath stroke];
    
    • bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:

    1.以可选哪个角为圆角矩形路径初始化创建并返回一个新的UIBezierPath对象

     UIBezierPath * recRoundCorPath=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 270, 80, 50) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
        [recRoundCorPath stroke];
    
    • bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:

    1.以圆弧路径初始化创建并返回一个新的UIBezierPath对象

    /*
         // 参数一: 圆弧圆心
         // 参数二: 圆弧半径
         // 参数三: 开始弧度
         // 参数四: 结束弧度
         // 参数五: 是否为顺时针
         */
        UIBezierPath * arcPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(80, 390) radius:60 startAngle:0 endAngle:M_PI/2 clockwise:NO];
        [arcPath fill];
    
    • bezierPathWithCGPath:

    1.以Core Graphics path的路径为参数创建并返回一个新的UIBezierPath对象

    • bezierPathByReversingPath

    1.使用当前路径的反向内容创建和返回一个新的贝塞尔路径对象

    • 构建路径

    • moveToPoint:

    1.将当前点移动到指定的位置

    • addLineToPoint:

    1.从当前点增加一条直线到指定点

    UIBezierPath * linePath = [UIBezierPath bezierPath];
    [linePath moveToPoint:CGPointMake(100, 10)]; [linePath addLineToPoint:CGPointMake(150, 100)];
    [linePath stroke];
    
    • addArcWithCenter:radius:startAngle:endAngle:clockwise:

    1.在路径中增加一条圆弧

    UIBezierPath * addArcPath =[UIBezierPath bezierPath];
        [addArcPath moveToPoint:CGPointMake(170, 300)];
        [addArcPath addLineToPoint:CGPointMake(300, 300)];
        [addArcPath addArcWithCenter:CGPointMake(235, 350) radius:40 startAngle:0 endAngle:M_PI clockwise:YES];
        [addArcPath stroke];
    
    • addQuadCurveToPoint:controlPoint:

    1.在路径中增加一条二次贝塞尔曲线


    WechatIMG418.jpeg
    UIBezierPath *bezierpath1 = [UIBezierPath bezierPath];
        [bezierpath1 moveToPoint:CGPointMake(200, 500)];
        // 参数一: 曲线的终点位置
        // 参数二: 控制点
        [bezierpath1 addQuadCurveToPoint:CGPointMake(300, 500) controlPoint:CGPointMake(210, 400)];
        
        [bezierpath1 stroke];
    
    • addCurveToPoint:controlPoint1:controlPoint2:

    在路径中增加一条三次贝塞尔曲线


    WechatIMG413.jpeg
    UIBezierPath *bezierpath2 = [UIBezierPath bezierPath];
        [bezierpath2 moveToPoint:CGPointMake(50, 640)];
        // 参数一: 曲线的终点位置
        // 参数二: 控制点1
        //参数三:控制点2
        
        //[bezierpath1 addQuadCurveToPoint:CGPointMake(300, 500) controlPoint:CGPointMake(210, 400)];
        [bezierpath2 addCurveToPoint:CGPointMake(450, 640) controlPoint1:CGPointMake(120, 410) controlPoint2:CGPointMake(300, 530)];
        [bezierpath2 stroke];
    
    • closePath

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

    • removeAllPoints

    移除这个UIBezierPath对象路径中所有的点

    • appendPath:

    在路径中增加一个已有UIBezierPath路径

    • CGPath

    Core Graphics 的路径表示

    • currentPoint

    图形路径中的当前点。

    • 访问图形属性

    • lineWidth

    线宽定义接收器的被触摸路径的厚度。0的宽度被解释为可以在特定设备上呈现的最薄的行。实际绘制的线宽可能与指定的宽度相差多达2个设备像素,这取决于线相对于像素网格和当前反锯齿设置的位置。行的宽度也可能受到活动图形上下文当前转换矩阵中指定的缩放因子的影响,默认的值为1

    • lineCapStyle

    路径的终点形状, 该属性适用于开放路径的起点和终点, 默认为kCGLineCapButt(方形结束, 结束位置正好为精确位置), 其他可选项为kCGLineCapRound(圆形结束, 结束位置超过精确位置半个线宽)和kCGLineCapSquare(方形结束, 结束位置超过精确位置半个线宽)

    • lineJoinStyle

    路径的连接点形状, 默认为kCGLineJoinMiter(全部连接), 其他可选项为kCGLineJoinRound(圆形连接)和kCGLineJoinBevel(斜角连接)

    • flatness

    平坦度值测量真实曲线上的点和绘制曲线上的点之间最大的允许距离(以像素为单位)。较小的值会导致更平滑的曲线,但需要更多的计算时间。较大的值会导致更多的锯齿曲线,但渲染速度更快。默认平面度值为0.6。
    在大多数情况下,不应改变平面度值。不过,您可能暂时增加平面度值,以最小化临时绘制形状所需的时间(例如在滚动过程中)

    • 绘制路径

    • fill

    利用当前绘画属性填充路径封闭范围, 该方法在绘画之前会自动将开放子路径封闭, 填充部分不包含路径本身, 所以对于线宽较大的路径, 填充部分会跟部分路径重合

    • fillWithBlendMode:alpha:

    利用指定模式填充路径封闭范围, 该方法在绘画之前会自动将开放子路径封闭, 填充部分不包含路径本身, 所以对于线宽较大的路径, 填充部分会跟部分路径重合

    • stroke

    利用当前绘画属性沿着路径画线

    • strokeWithBlendMode:alpha:

    利用指定模式沿着路径画线

    • 裁剪路径

    • addClip

    没怎么用过

    • Hit Detection

    • containsPoint:

    返回一个布尔值,包含的区域是否包含指定的点

    • empty

    是否路径信息为空, 即使通过moveToPoint:移动到指定的位置也算不为空

    • bounds

    可以封闭所有路径点的最小矩形范围, 包括多次贝塞尔曲线的控制点在内

    相关文章

      网友评论

        本文标题:UIBezierPath

        本文链接:https://www.haomeiwen.com/subject/xqmphxtx.html