Move CGPathCreateMutable() so the path stays the same?

血红的双手。 提交于 2019-12-05 01:11:55

问题


I have got 9 hexagon sprites across the screen and I have to specify a certain path around them to make their area touchable. I don't want to set the coordinates for every single path respectively, so can't I just use the first path (the hexagons are all the same size) and move its origin to another position (without destroying the shape)? (If i do it now, the CGPathAddLineToPoint(); adds the points of the hexagon from the previous hexagon. I hope I got the idea across ... (see picture... NOTE : The grey octagon on the upper right is the exact same size and shape as the black one)![Move path coordinates and shape][1]

hex2TouchArea=CGPathCreateMutable();

    CGPathMoveToPoint(hex2TouchArea, NULL, 150,157);
    CGPathAddLineToPoint(hex2TouchArea, NULL, 130, 198);
    CGPathAddLineToPoint(hex2TouchArea, NULL, 146, 236);
    CGPathAddLineToPoint(hex2TouchArea, NULL, 195, 236);
    CGPathAddLineToPoint(hex2TouchArea, NULL, 218, 197);
    CGPathAddLineToPoint(hex2TouchArea, NULL, 193, 157);
    CGPathCloseSubpath(hex2TouchArea);

here i put a picture that shows it in an image
http://666kb.com/i/bz2ysevuw8n65rh3i.gif

*EDIT: I got the solution from the post and changed it a little bit : *

-(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, NULL, newloc.x, newloc.y);
    CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
    CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
    CGPathAddLineToPoint(path, NULL, newloc.x + 23,  newloc.y - 39);
    CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40);
    CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0);
    CGPathCloseSubpath(path);
    return path;   

}

回答1:


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.



来源:https://stackoverflow.com/questions/8310378/move-cgpathcreatemutable-so-the-path-stays-the-same

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