如何在iOS中用渐变填充CGPoints定义的形状?
Posted
技术标签:
【中文标题】如何在iOS中用渐变填充CGPoints定义的形状?【英文标题】:How to fill a shape defined with CGPoints with gradient in iOS? 【发布时间】:2011-08-24 13:04:09 【问题描述】:我在代码中设置了自定义形状的箭头。我想做的是用渐变填充它。问题是,我不知道如何用渐变填充非矩形形状(暗框内的空间)。有什么想法吗?
//Define colours used in drawing
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef lightColor = _lightColor.CGColor;
CGColorRef darkColor = _darkColor.CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:0.2 green:0.2
blue:0.2 alpha:0.5].CGColor;
//Get label text size to help determine sizes for drawing
CGSize textSize = [[_titleLabel text] sizeWithFont:[_titleLabel font]];
//Set shadow
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3.0, shadowColor);
//Set arrow shape
CGPoint rectangle_points[] =
CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y),
CGPointMake(textSize.width+10, _coloredBoxRect.origin.y),
CGPointMake(textSize.width+40, _coloredBoxRect.origin.y+20),
CGPointMake(textSize.width+10, _coloredBoxRect.origin.y+40),
CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y+40),
CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y),
;
CGContextAddLines(context, rectangle_points, 6);
CGContextSetFillColorWithColor(context, lightColor);
CGContextFillPath(context);
CGContextRestoreGState(context);
//Draw dark frame for the arrow
CGContextSetStrokeColorWithColor(context, darkColor);
CGContextSetLineWidth(context, 1.0);
CGContextSaveGState(context);
draw1PxStroke(context, CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y), CGPointMake(textSize.width+10, _coloredBoxRect.origin.y), darkColor);
draw1PxStroke(context, CGPointMake(textSize.width+10, _coloredBoxRect.origin.y+40), CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y+40), darkColor);
draw1PxStroke(context, CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y), CGPointMake(_coloredBoxRect.origin.x, _coloredBoxRect.origin.y+40), darkColor);
draw1PxStroke(context, CGPointMake(textSize.width+10, _coloredBoxRect.origin.y), CGPointMake(textSize.width+40, _coloredBoxRect.origin.y+20), darkColor);
draw1PxStroke(context, CGPointMake(textSize.width+10, _coloredBoxRect.origin.y+40), CGPointMake(textSize.width+40, _coloredBoxRect.origin.y+20), darkColor);
CGContextRestoreGState(context);
【问题讨论】:
【参考方案1】:查看这个苹果示例应用程序。 它正是您在“多边形”部分(用于填充多边形)中所需要的。 您需要更改的是绘制渐变而不是像示例中那样绘制。 在“渐变”部分下的示例中还举例说明了绘制渐变。
http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html
希望这会有所帮助, 弗拉德
【讨论】:
以上是关于如何在iOS中用渐变填充CGPoints定义的形状?的主要内容,如果未能解决你的问题,请参考以下文章