如何在iOS中用渐变填充用CGPoints定义的形状? [英] How to fill a shape defined with CGPoints with gradient in iOS?

查看:107
本文介绍了如何在iOS中用渐变填充用CGPoints定义的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中设置了自定义箭头形状。我想要做的是用渐变填充它。问题是,我不知道如何用渐变填充非矩形形状(暗框内的空间)。有什么想法?

I have a custom shape of an arrow set up in code. What I'm trying to do is fill it with gradient. Problem is, I have no idea how to fill a non rectangular shape (space inside dark frame) with gradient. Any ideas?

 //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);


推荐答案

查看此Apple示例应用程序。
它完全符合你所需要的多边形部分(用于填充多边形)。
您需要更改的是绘制渐变而不是绘图,如示例中所示。
在渐变部分的示例中还举例说明了绘制渐变。

Check out this apple sample application. It has exactly what u need in the "Polygons" section (for filling a polygon). What you need to change is drawing the gradient instead of drawing as in the example. Drawing gradients is also exemplified in the sample, under the "Gradients" section.

http://developer.apple.com/library/ios/#samplecode/QuartzDemo/Introduction/Intro.html

希望这会有所帮助,
Vlad

Hope this helps, Vlad

这篇关于如何在iOS中用渐变填充用CGPoints定义的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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