Move CGPathCreateMutable() so the path stays the same?

烈酒焚心 提交于 2019-12-03 16:25:35

The best way of reusing a path is probably make a method for them. Make one where you add the start coordinates and return a CGMutablePathRef so you can draw it after the path is done.

Here is what is would look like based on the example path you put in your question:

-(CGMutablePathRef) drawHexagon:(CGPoint)origin
{
    //create mutable path
    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, nil, origin.x, origin.y);

    CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);
    CGPoint newloc = CGPointMake(newloc.x + 16, newloc.y + 38);
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);
    CGPoint newloc = CGPointMake(newloc.x + 49, newloc.y + 0);
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);
    CGPoint newloc = CGPointMake(newloc.x + 23, newloc.y - 39);
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);
    CGPoint newloc = CGPointMake(newloc.x - 25, newloc.y - 40);
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);
    CGPoint newloc = CGPointMake(newloc.x - 43, newloc.y + 0); //which should be you origin
    CGPathMoveToPoint(path, nil, newloc.x, newloc.y);

    CGPathCloseSubpath(path);
    return path;   
}

call it with CGMutablePathRef path = [self drawHexagon:someStartingPoint];


Editted due comments:

You can add the path to the context with: CGContextAddPath(context, path); Then draw it however you feel like, for example like this: CGContextDrawPath(context, kCGPathFill);

It shouldn't be hard after you added the path to the context.

This should work for you. Good luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!