博客> CoreGraphics☞CGPath
CoreGraphics☞CGPath
2017-08-16 18:48 评论:0 阅读:437 M_Sunxxnny
CGPath详解 coreGraphics
/**
 *  @author kongshuang, 16-06-28 12:06:01
 *
 *  @brief CGPath 方法详解
 *
 *  CGPath方法有相当大一部分跟CGContext相似
 *  CGMutablePathRef 和CGPathRef的区别在与可变和不可变,CGMutablePathRef 可用于 path的construction function和construction convenience function .
 *
 *  @return CGPathRef
 */
-(CGPathRef)testCGPath{

    CGMutablePathRef mutablePathRef0=NULL;//可变path
    CGPathRef pathRef0=NULL;//不可变path
    CGPathRef pathRef1=NULL;

    //create path
     //创建CGMutablePathRef
    mutablePathRef0=CGPathCreateMutable();
    //mutablePathRef 添加矩形路径
    CGPathAddRoundedRect(mutablePathRef0, NULL, CGRectMake(100, 100, 100, 100), 0, 0);
    CGPathAddRoundedRect(mutablePathRef0, NULL, CGRectMake(200, 200, 100, 100), 0, 0);
    //根据矩形创建path
    pathRef0=CGPathCreateWithRect(CGRectMake(10, 10, 100, 100), nil);
    //圆角矩形path
    pathRef0=CGPathCreateWithRoundedRect(CGRectMake(10, 10, 100, 100), 30, 10, NULL);
    //椭圆path
    pathRef0=CGPathCreateWithEllipseInRect(CGRectMake(10, 10, 100, 100), NULL);
    //copy path to CGPathRef
    pathRef1=CGPathCreateMutableCopy(pathRef0);

    //copy path to CGPathRef and 变换
    pathRef1=CGPathCreateMutableCopyByTransformingPath(pathRef0, NULL);
    //copy path
    pathRef1=CGPathCreateCopy(CGPathCreateWithRect(CGRectMake(10, 10, 100, 100), nil));
    //copy path 变成虚线path
    CGFloat lenghts[]={10,30,40};
    pathRef1=CGPathCreateCopyByDashingPath(pathRef0, NULL, 1,lenghts, 1);
    //copy path 并且设置线条样式
    pathRef1=CGPathCreateCopyByStrokingPath(pathRef0, NULL, 10, kCGLineCapButt, kCGLineJoinMiter, 0);
    //copy path 并且设置变换
    pathRef1=CGPathCreateCopyByTransformingPath(pathRef0, nil);

    //通过Retain获取新path
    pathRef1=CGPathRetain(pathRef0);
    //对比两个CGPathRef
   NSLog(@"cgpathEqual to path:%d",CGPathEqualToPath(pathRef0, pathRef1));
    //释放CGPathRef
//    CGPathRelease(pathRef0);
    CGPathRelease(pathRef1);
    //error CGPathRelease 不能传递CGMutableCGPathRef
//    CGPathRelease(mutablePathRef0);

    /*** Path construction functions. ***/

    //移动到点path
    CGPathMoveToPoint(mutablePathRef0, NULL, 120, 300 );
    //点到点直线path
    CGPathAddLineToPoint(mutablePathRef0, NULL, 140, 200);
    //二次曲线path
    CGPathAddQuadCurveToPoint(mutablePathRef0, NULL, 0.12, 0.41, 150, 400);
    //贝塞尔曲线path
    CGPathAddCurveToPoint(mutablePathRef0, NULL,  0.17, 0.67, 0.97, 0.19, 50, 500);
    //连接到起点
    CGPathCloseSubpath(mutablePathRef0);

    /*** Path construction convenience functions. ***/

    //添加矩形path
    CGPathAddRect(mutablePathRef0, NULL, CGRectMake(200, 200, 30, 30));
    //添加多个矩形path
    CGRect CGRects[]={CGRectMake(100, 200, 40, 40),CGRectMake(100, 250, 40, 40),CGRectMake(100, 300, 40, 40)};
    CGPathAddRects(mutablePathRef0, NULL, CGRects, 3);
    //添加多个点确定的直线path
    CGPoint points[]={CGPointMake(10, 10),CGPointMake(40, 40),CGPointMake(230, 100)};
    CGPathAddLines(mutablePathRef0, NULL, points, 3);
    //添加椭圆path
    CGPathAddEllipseInRect(mutablePathRef0, NULL, CGRectMake(30, 400, 100, 30));
    //圆弧
    CGPathAddRelativeArc(mutablePathRef0, NULL, 200, 400, 50, 0, -2);
    CGPathAddArc(mutablePathRef0, NULL, 200, 500, 30, 0, M_PI, 1);
    CGPathAddArcToPoint(mutablePathRef0, NULL, 300, 400, 200, 200, 100);
    //添加path到mutablePath
    CGPathAddPath(mutablePathRef0, NULL, pathRef0);

    /*** Path information functions. ***/
    //path 是否为空
    NSLog(@"CGPathIsEmpty pathRef:%d",CGPathIsEmpty(pathRef0));
    //path是否为矩形
    CGRect rect=CGRectMake(100, 100, 100, 100);
    NSLog(@"CGPathIsRect pathRef0:%d",CGPathIsRect(pathRef0,&rect));
    //获取path的现在节点
    NSLog(@"pathRef0 current point:%@",NSStringFromCGPoint(CGPathGetCurrentPoint(pathRef0)));
    //获取path边界
    NSLog(@"CGPathGetBoundingBox(pathRef0):%@",NSStringFromCGRect(CGPathGetBoundingBox(pathRef0)));
    //获取path边界
    NSLog(@"CGPathGetPathBoundingBox(pathRef0):%@",NSStringFromCGRect(CGPathGetPathBoundingBox(pathRef0)));
    //判断path是否包含某个点
    NSLog(@"CGPathContainsPoint:%d",CGPathContainsPoint(pathRef0, NULL, CGPointMake(100, 100), YES));

    return mutablePathRef0;
}
收藏
2
sina weixin mail 回到顶部