如何在iPhone上绘制一个带有剪裁而非圆角的矩形?

如何在iPhone上绘制一个带有剪裁而非圆角的矩形?,iphone,quartz-graphics,Iphone,Quartz Graphics,我看到了许多使用iphonesdk绘制圆形矩形的示例。我真正需要的是一个修剪过的角矩形,如下所示: 谢谢, Josh我只会使用8条线段(起始路径、向路径添加直线、结束路径、笔划路径)。 您只需从每个x或y角坐标中添加或减去一些常量,即可获得所有8个点。您可以使用与CGStrokeRect相同的api编写一个简单的函数来封装上述所有内容。我只需要使用8个线段(起始路径、添加线到路径、结束路径、笔划路径)。 您只需从每个x或y角坐标中添加或减去一些常量,即可获得所有8个点。您可以使用与CGStro

我看到了许多使用iphonesdk绘制圆形矩形的示例。我真正需要的是一个修剪过的角矩形,如下所示:

谢谢,
Josh

我只会使用8条线段(起始路径、向路径添加直线、结束路径、笔划路径)。
您只需从每个x或y角坐标中添加或减去一些常量,即可获得所有8个点。您可以使用与CGStrokeRect相同的api编写一个简单的函数来封装上述所有内容。

我只需要使用8个线段(起始路径、添加线到路径、结束路径、笔划路径)。 您只需从每个x或y角坐标中添加或减去一些常量,即可获得所有8个点。您可以使用与CGStrokeRect相同的api编写一个简单的函数来封装上述所有内容。

(这是根据Jonathan Grynspan的建议编辑的,只需使用helper函数来创建路径。它现在还允许修剪角点的高度与其宽度不同。)

下面是一个帮助器C函数,用于创建这样的路径:

// Note: caller is responsible for releasing the returned path
CGPathRef createAngledCornerRectPath(CGRect rect,
                                     CGSize cornerSize,
                                     CGFloat strokeWidth)
{
    CGMutablePathRef p = CGPathCreateMutable();

    // Make points for the corners
    CGFloat inset = strokeWidth/2; // because the stroke is centered on the path.
    CGPoint tlc = CGPointMake(CGRectGetMinX(rect) + inset,
                              CGRectGetMinY(rect) + inset);
    CGPoint trc = CGPointMake(CGRectGetMaxX(rect) - inset,
                              CGRectGetMinY(rect) + inset);
    CGPoint blc = CGPointMake(CGRectGetMinX(rect) + inset,
                              CGRectGetMaxY(rect) - inset);
    CGPoint brc = CGPointMake(CGRectGetMaxX(rect) - inset,
                              CGRectGetMaxY(rect) - inset);

    // Start in top left and move around counter-clockwise.
    CGPathMoveToPoint(p, NULL, tlc.x, tlc.y+cornerSize.height);
    CGPathAddLineToPoint(p, NULL, blc.x, blc.y-cornerSize.height);
    CGPathAddLineToPoint(p, NULL, blc.x+cornerSize.width, blc.y);
    CGPathAddLineToPoint(p, NULL, brc.x-cornerSize.width, brc.y);
    CGPathAddLineToPoint(p, NULL, brc.x, brc.y-cornerSize.height);
    CGPathAddLineToPoint(p, NULL, trc.x, trc.y+cornerSize.height);
    CGPathAddLineToPoint(p, NULL, trc.x-cornerSize.width, trc.y);
    CGPathAddLineToPoint(p, NULL, tlc.x+cornerSize.width, trc.y);
    CGPathCloseSubpath(p);

    return p;
}
下面是如何在自定义视图的-drawRect:方法中使用它:

- (void)drawRect:(CGRect)rect
{
    // Define a few parameters
    CGSize cornerSize = CGSizeMake(30.f, 30.f);
    CGFloat strokeWidth = 3.f;
    CGColorRef strokeColor = [UIColor redColor].CGColor;

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(c, strokeColor);
    CGContextSetLineWidth(c, strokeWidth);

    // Create the path, add it to the context, and stroke it.
    CGPathRef path = createAngledCornerRectPath(rect,
                                                cornerSize,
                                                strokeWidth);
    CGContextAddPath(c, path);
    CGContextStrokePath(c);

    // we are responsible for releasing the path
    CGPathRelease(path);
}
(这是根据Jonathan Grynspan的建议编辑的,只需使用helper函数来创建路径。它现在还允许修剪角的高度与其宽度不同。)

下面是一个帮助器C函数,用于创建这样的路径:

// Note: caller is responsible for releasing the returned path
CGPathRef createAngledCornerRectPath(CGRect rect,
                                     CGSize cornerSize,
                                     CGFloat strokeWidth)
{
    CGMutablePathRef p = CGPathCreateMutable();

    // Make points for the corners
    CGFloat inset = strokeWidth/2; // because the stroke is centered on the path.
    CGPoint tlc = CGPointMake(CGRectGetMinX(rect) + inset,
                              CGRectGetMinY(rect) + inset);
    CGPoint trc = CGPointMake(CGRectGetMaxX(rect) - inset,
                              CGRectGetMinY(rect) + inset);
    CGPoint blc = CGPointMake(CGRectGetMinX(rect) + inset,
                              CGRectGetMaxY(rect) - inset);
    CGPoint brc = CGPointMake(CGRectGetMaxX(rect) - inset,
                              CGRectGetMaxY(rect) - inset);

    // Start in top left and move around counter-clockwise.
    CGPathMoveToPoint(p, NULL, tlc.x, tlc.y+cornerSize.height);
    CGPathAddLineToPoint(p, NULL, blc.x, blc.y-cornerSize.height);
    CGPathAddLineToPoint(p, NULL, blc.x+cornerSize.width, blc.y);
    CGPathAddLineToPoint(p, NULL, brc.x-cornerSize.width, brc.y);
    CGPathAddLineToPoint(p, NULL, brc.x, brc.y-cornerSize.height);
    CGPathAddLineToPoint(p, NULL, trc.x, trc.y+cornerSize.height);
    CGPathAddLineToPoint(p, NULL, trc.x-cornerSize.width, trc.y);
    CGPathAddLineToPoint(p, NULL, tlc.x+cornerSize.width, trc.y);
    CGPathCloseSubpath(p);

    return p;
}
下面是如何在自定义视图的-drawRect:方法中使用它:

- (void)drawRect:(CGRect)rect
{
    // Define a few parameters
    CGSize cornerSize = CGSizeMake(30.f, 30.f);
    CGFloat strokeWidth = 3.f;
    CGColorRef strokeColor = [UIColor redColor].CGColor;

    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(c, strokeColor);
    CGContextSetLineWidth(c, strokeWidth);

    // Create the path, add it to the context, and stroke it.
    CGPathRef path = createAngledCornerRectPath(rect,
                                                cornerSize,
                                                strokeWidth);
    CGContextAddPath(c, path);
    CGContextStrokePath(c);

    // we are responsible for releasing the path
    CGPathRelease(path);
}

什么图书馆?你可以使用OpenGLES或者CGGeometry(或者甚至Cocos2d,但你可能不这么做)。这并不重要。只是用什么库寻找最简单的解决方案?你可以使用OpenGLES或者CGGeometry(或者甚至Cocos2d,但你可能不这么做)。这并不重要。寻找最简单的解决方案这很好,但我会将其进一步推广到一个返回
CGPath
对象的函数中--这样,它可以用于填充形状、绘制形状、剪裁形状等。这很好,但我会将其进一步推广到一个返回
CGPath
对象的函数中--这样,它可以用于整体填充形状、笔划形状、剪裁形状等。