美文网首页
【CoreGraphics】CGPath - 路径:比UIKit

【CoreGraphics】CGPath - 路径:比UIKit

作者: 居然是村长 | 来源:发表于2016-03-30 22:04 被阅读3298次

    这仅仅是路径,需要添加到画布上才能显示
    这里 全部没有使用 CGAffineTransform,2D 变化(仿射变化)另外再说。

    不可变路径创建 CGPathRef

        CGPathRef pathRef = CGPathCreateWithRect(rect, nil);
        CGPathCreateWithEllipseInRect(rect, nil);
        CGPathCreateWithRoundedRect(rect, 10, 20, nil);
    
        CGPathCreateCopy(pathRef);
        CGPathCreateCopyByTransformingPath(pathRef, nil);
        CGPathCreateCopyByStrokingPath(pathRef, nil, 2, kCGLineCapButt, kCGLineJoinMiter, 4);
        /*
        typedef CF_ENUM(int32_t, CGLineJoin) {
            kCGLineJoinMiter,
            kCGLineJoinRound,
            kCGLineJoinBevel
        };
        
        typedef CF_ENUM(int32_t, CGLineCap) {
            kCGLineCapButt,
            kCGLineCapRound,
            kCGLineCapSquare
        };
        */
    

    可变路径创建 CGMutablePathRef ,下面连点绘制

        CGMutablePathRef pathRef = CGPathCreateMutable();
        CGPathCreateMutableCopy(pathRef);
        CGPathCreateMutableCopyByTransformingPath(pathRef, nil);
        
    

    路径绘制

    基本与UIBezierPath 方法一致,就是多了两条线的圆角。

    // 可变路径 用于添加下一个点
        CGMutablePathRef pathRef = CGPathCreateMutable();
    
    起点
        CGPathMoveToPoint(pathRef, nil, 10, 20);
    
    直线
        CGPathAddLineToPoint(pathRef, nil, 100, 200);
    
    一次贝塞尔 控制点 终点
        CGPathAddQuadCurveToPoint(pathRef, nil, 50, 50, 100, 200);
    
    二次贝塞尔曲线 控制点1 控制点2 终点
        CGPathAddCurveToPoint(pathRef, nil, 30, 30, 10, 200, 200, 300);
    
    闭合当前点 和 起点
        CGPathCloseSubpath(pathRef);
    
    
    
    矩形
        CGPathAddRect(pathRef, nil, CGRectMake(100, 100, 100, 100));
    
    // 圆角矩形 rect , 圆角 宽 高
        CGPathAddRoundedRect(pathRef, nil, CGRectMake(100, 100, 100, 100), 10, 40);
    
    内切椭圆
        CGPathAddEllipseInRect(pathRef, nil, CGRectMake(100, 100, 100, 100));
        
        
    弧线 (x,y)圆心 ,半径,起点  圆心角
        CGPathAddRelativeArc(pathRef, nil, 200, 200, 50, 0, M_PI/2);
    
    同上 并增加 顺逆时针(上面的是画短的那条)(这个可以画出长的那条)
        CGPathAddArc(pathRef, nil, 200, 200, 50, 0, M_PI/2,YES);
    
    -------------------------注意这个 UIBezierPath不好画,其他都是差不多的 ----------------------------
    圆角 :起点-(x1,y1)的直线  与 (x1,y1)-(x2,y2) 内切的圆弧 
        CGPathAddArcToPoint(pathRef, nil, 100, 100, 200, 200, 10);
    -------------------------注意这个 UIBezierPath不好画,其他都是差不多的 ----------------------------
    
    
    合并 路径
        CGPathAddPath(pathRef, nil, pathRef2);
    
    

    路径信息

        // 返回 BOOl
        CGPathIsEmpty(pathRef);
        CGPathEqualToPath(pathRef1, pathRef2);
        CGPathIsRect(pathRef, &rect);
        CGPathContainsPoint(pathRef, nil, CGPointMake(100, 100), YES);// 最后的 YES 与 填充规则有关
        
        // 返回 对应的 结构体
        CGPathGetCurrentPoint(pathRef);
        CGPathGetBoundingBox(pathRef);// 包括内赛尔曲线的 控制点
        CGPathGetPathBoundingBox(pathRef);// 不包括 控制点
    

    注意 Release

        CGPathRetain(pathRef);
        CGPathRelease(pathRef);// 释放
    

    非本节内容,但是为了整体性测试,添加到画布看看效果。

    
        CAShapeLayer *newlayer = [[CAShapeLayer alloc] init];
        newlayer.path = pathRef;// 把上面绘制的 pathRef 赋值给画布!
        CFRelease(pathRef);// 这里 release了。
        newlayer.fillColor = [UIColor whiteColor].CGColor;
        newlayer.strokeColor = [UIColor redColor].CGColor;
        newlayer.lineWidth = 1.;
        [self.layer insertSublayer:newlayer atIndex:0];
    

    其他

    /* Return the CFTypeID for CGPathRefs. */
    CG_EXTERN CFTypeID CGPathGetTypeID(void)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
     
    /* Create a dashed path from `path'. The parameters `phase', `lengths', and
       `count' have the same meaning as the corresponding parameters for
       `CGContextSetLineDash'. If `transform' is non-NULL, then the elements of
       the constructed path will be transformed by `transform' before they are
       added to the path. */
    CG_EXTERN CGPathRef __nullable CGPathCreateCopyByDashingPath(
        CGPathRef __nullable path, const CGAffineTransform * __nullable transform,
        CGFloat phase, const CGFloat * __nullable lengths, size_t count)
        CG_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_5_0);
    
     
    /* Add each rectangle specified by `rects', an array of `count' CGRects, to
       `path'. If `m' is non-NULL, then first transform each rectangle by `m'
       before adding it to `path'. */
    CG_EXTERN void CGPathAddRects(CGMutablePathRef __nullable path,
        const CGAffineTransform * __nullable m, const CGRect * __nullable rects,
        size_t count)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    /* Move to the first element of `points', an array of `count' CGPoints, and
       append a line from each point to the next point in `points'. If `m' is
       non-NULL, then first transform each point by `m'. */
    CG_EXTERN void CGPathAddLines(CGMutablePathRef __nullable path,
        const CGAffineTransform * __nullable m, const CGPoint * __nullable points,
        size_t count)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
     
    
    /* The types of path elements returned by `CGPathApply'. */
    typedef CF_ENUM(int32_t, CGPathElementType) {
      kCGPathElementMoveToPoint,
      kCGPathElementAddLineToPoint,
      kCGPathElementAddQuadCurveToPoint,
      kCGPathElementAddCurveToPoint,
      kCGPathElementCloseSubpath
    };
    
    /* An element of a path returned by `CGPathApply'. */
    struct CGPathElement {
      CGPathElementType type;
      CGPoint *  points;
    };
    typedef struct CGPathElement CGPathElement;
    
    /* The prototype for the function called by `CGPathApplyFunction'. */
    typedef void (*CGPathApplierFunction)(void * __nullable info,
      const CGPathElement *  element);
    
    /* For element of `path', call `function', passing it the path element and
       `info'. */
    CG_EXTERN void CGPathApply(CGPathRef __nullable path, void * __nullable info,
        CGPathApplierFunction __nullable function)
        CG_AVAILABLE_STARTING(__MAC_10_2, __IPHONE_2_0);
    
    

    相关文章

      网友评论

          本文标题:【CoreGraphics】CGPath - 路径:比UIKit

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