簡體   English   中英

如何在iOS中使用漸變填充CGPoints定義的形狀?

[英]How to fill a shape defined with CGPoints with gradient in iOS?

我在代碼中設置了一個自定義的箭頭形狀。 我想做的是用漸變填充它。 問題是,我不知道如何用漸變填充非矩形形狀(黑框內的空間)。 有任何想法嗎?

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

查看這個蘋果示例應用程序。 它正是您在“多邊形”部分(填充多邊形)所需要的。 您需要更改的是繪制漸變,而不是像示例中那樣繪制。 樣本中“漸變”部分下的繪制漸變也得到了示例。

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

希望這對您有幫助,弗拉德

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM