-1

当我在 xcode for ios 中绘制形状时,如何更改最终绘制形状的颜色?

- (void) drawArrowWithContext:(CGContextRef)context atPoint:(CGPoint)startPoint withSize:   (CGSize)size lineWidth:(float)width arrowHeight:(float)aheight
{
float width_wing = (size.width-width)/2;
float main = size.height-aheight;
CGPoint rectangle_points[] =
{
    CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
    CGPointMake(startPoint.x + width_wing, startPoint.y + main),
    CGPointMake(startPoint.x + 0.0, startPoint.y + main), // left point
    CGPointMake(startPoint.x + size.width/2, startPoint.y + size.height),

    CGPointMake(startPoint.x + size.width, startPoint.y + main), // right point

    CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + main),

    CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + 0.0),
    CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
};

CGContextAddLines(context, rectangle_points, 8);

CGContextFillPath(context);
}

默认情况下,此箭头的颜色为黑色,但我希望用户能够将其更改为黑色、白色或红色。我怎样才能做到这一点?

4

2 回答 2

2

我认为您正在寻找的是CGContextSetRGBFillColorCGContextSetRGBStrokeColor

- (void) drawArrowWithContext:(CGContextRef)context atPoint:(CGPoint)startPoint withSize: (CGSize)size lineWidth:(float)width arrowHeight:(float)aheight andColor:(UIColor *)color
{
float width_wing = (size.width-width)/2;
float main = size.height-aheight;

CGContextSetRGBFillColor(context,color.CGColor);
CGContextSetRGBStrokeColor(context,color.CGColor);

CGPoint rectangle_points[] =
{
    CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
    CGPointMake(startPoint.x + width_wing, startPoint.y + main),
    CGPointMake(startPoint.x + 0.0, startPoint.y + main), // left point
    CGPointMake(startPoint.x + size.width/2, startPoint.y + size.height),

    CGPointMake(startPoint.x + size.width, startPoint.y + main), // right point

    CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + main),

    CGPointMake(startPoint.x + size.width-width_wing, startPoint.y + 0.0),
    CGPointMake(startPoint.x + width_wing, startPoint.y + 0.0),
};

CGContextAddLines(context, rectangle_points, 8);

CGContextFillPath(context);
}
于 2013-01-22T16:47:11.793 回答
0

您需要用新颜色重新绘制它。箭头不作为具有可以动态更改的属性的对象存在。

因此,更改您的 @selector 以接受您通过CGContextSetRGBFillColoror应用的颜色参数CGContextSetRGBStrokeColor。这可以是CGColor或整数,您可以在 switch 语句中使用它来从一些预定义的颜色中选择。

于 2013-01-22T16:51:57.297 回答