当我在xos中为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);
}
默认情况下,此箭头的颜色为黑色,但我希望用户能够将其更改为黑色,白色或红色。我怎么能这样做?
答案 0 :(得分:2)
我认为您要找的是CGContextSetRGBFillColor
或CGContextSetRGBStrokeColor
- (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);
}
答案 1 :(得分:0)
您需要使用新颜色重绘它。箭头不作为具有可以动态更改的属性的对象存在。
因此,请更改@selector以接受通过CGContextSetRGBFillColor
或CGContextSetRGBStrokeColor
应用的颜色参数。这可以是CGColor,也可以是整数,您可以在switch语句中使用它来从某些预定义的颜色中进行选择。