移动 CGPathCreateMutable() 使路径保持不变? [英] Move CGPathCreateMutable() so the path stays the same?

查看:65
本文介绍了移动 CGPathCreateMutable() 使路径保持不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕上有 9 个六边形精灵,我必须在它们周围指定特定的路径才能使它们的区域可触摸.我不想分别为每条路径设置坐标,所以我不能只使用第一条路径(六边形都是相同的大小)并将其原点移动到另一个位置(不破坏形状)?(如果我现在这样做, CGPathAddLineToPoint(); 从前一个六边形添加六边形的点.我希望我明白了......(见图......注意:右上角的灰色八角形是与黑色完全相同的大小和形状)![移动路径坐标和形状][1]

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();

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);

这里我放了一张图片,在图像中显示它
http://666kb.com/i/bz2ysevuw8n65rh3i.gif

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

*我从帖子中得到了解决方案并对其进行了一些更改:*

-(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;   

}

推荐答案

重用路径的最佳方式可能是为它们创建一个方法.在添加起始坐标的位置创建一个并返回 CGMutablePathRef 以便在路径完成后绘制它.

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;   
}

CGMutablePathRef path = [self drawHexagon:someStartingPoint];

编辑应有的评论:

您可以使用以下命令添加上下文的路径:CGContextAddPath(context, path);然后按照你的感觉绘制它,例如这样: CGContextDrawPath(context, kCGPathFill);

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.

这篇关于移动 CGPathCreateMutable() 使路径保持不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆