如何在CGContext CGMutablePathRef上绘制顶点? [英] How to draw the vertices on a CGContext CGMutablePathRef?

查看:134
本文介绍了如何在CGContext CGMutablePathRef上绘制顶点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入单击以指定顶点时,我正在绘制一组连接的线。我正在构建CGMutablePathRef

I am drawing a set of connected lines as the user enters clicks to specify vertices. I am build a CGMutablePathRef

- (void)addPointToCGMPR: (CGPoint)p
      forNewPolygon: (BOOL)newPoly
{
    if (newPoly)
    {
        CGPathMoveToPoint(cgmpr, NULL, p.x, p.y);
    }
    else
    {
        CGPathAddLineToPoint(cgmpr, NULL, p.x, p.y);
    }
    [self setNeedsDisplay];
}

drawRect:在输入每个点后调用,上下文CGMutablePathRef为添加到CGContext中进行显示

drawRect: is then called after each point is entered, and the context CGMutablePathRef is added to a CGContext for display

- (void)drawRect:(CGRect)rect
{    
    // Set up a context to display
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddPath(context, cgmpr);

    CGContextSetLineWidth(context, 1);
    CGContextStrokePath(context);  

    // If NOT empty then Close the context so that box comparison can occur later
    if (!CGContextIsPathEmpty(context))
    {
        CGContextClosePath(context);
    }
}

我在屏幕上看到的线条就像人们期望的那样。我的麻烦是,在用户选择了第一点之后,屏幕上没有呈现任何内容,并且直到输入第二点之后,用户都不知道系统是否获得了他的第一选择。我希望系统在输入下一个点之前就渲染第一个点。我还希望该系统以一种视觉上独特的方式渲染每个拾取的顶点-现在我没有渲染顶点,只有线。有没有办法让CGContext渲染点?有没有办法指定渲染这些点的样式?谢谢。

I am getting lines on the screen as one would expect. My trouble is that after the user picks the first point there is nothing rendered on the screen and the user is left not knowing if the system got his first pick until after the second point is entered. I would like for the system to render the first point even before the next point is entered. I would also like for the system to render each one of the vertices picked in a visually distinct way - right now I am not getting vertices rendered, only lines. Is there a way to ask the CGContext to render points? Is there a way to specify the style in which these points are rendered? Thanks.

推荐答案

您可以自由绘制想要代表点的任何东西:一个小正方形,一个小圆圈,一个图钉等的图像。您已经知道如何绘制上下文。只需循环显示您的要点并绘制它们即可,但是您希望它们出现。我个人倾向于使用 CGContextFillEllipseInRect 这样的东西。对于每个点,制作一个围绕该点的矩形并将其绘制:

You're free to draw whatever you want to represent the points: a small square, a small circle, an image of a pushpin, etc. You already know how to draw into a context; just loop over your points and draw them however you want them to appear. I personally tend to use CGContextFillEllipseInRect for things like this. For each point, make a rect that surrounds the point and draw it:

CGFloat pointSize = 4;
CGRect pointRect = CGRectMake(point.x - pointSize / 2, point.y - pointSize / 2, pointSize, pointSize);
CGContextFillEllipseInRect(context, pointRect);

这篇关于如何在CGContext CGMutablePathRef上绘制顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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