2012-01-09 5 views
1

CGContextAddArcを使用してiPhoneアプリで描画している閉じた図形をいくつか持っていますが、グラデーションを適用したいのですができません良い例を見つける。私は、クリップが描かれたシェイプリファレンスCGRectのどこかに縛られていることを知りましたが、グラデーション境界を非CGRectシェイプにクリップする必要があります。任意のアイデア/助け?AddArcで描画された非正方形にグラデーションを適用する

私はxcode 4.2.1とstoryboardとiOS5を使用していますが、これらの図形はビュー内でプログラムで描画されています。

私は私の非正方形を描画するために使用しているコード:あなたはCGContextClip機能、または多分CGContextEOClip機能を探している

CGContextRef context = UIGraphicsGetCurrentContext(); 

//set context-based constants 
double widthMiddle = self.frame.size.width/2; 
double heightMiddle = self.frame.size.height/2; 
double avgDimension = (widthMiddle + heightMiddle)/2; 
float arcRadius = avgDimension * .9; 
float innerRadius = avgDimension * .4; 

double startAngle = 2 * (sectionNumber - 1) * (M_PI/3); 
double endAngle = (2 * (sectionNumber * (M_PI/3))) - [sectionSpacing doubleValue]; 
double interfaceAngle = [sectionSpacing doubleValue] * (innerRadius/arcRadius); 
double ratingRadius = innerRadius + ((arcRadius-innerRadius) * percentGood); 
double percentInterfaceAngle = interfaceAngle * (1-percentGood); 

//NSLog(@"InterfaceAngle and percentInterfaceAngle are: %f/%f", interfaceAngle, percentInterfaceAngle); 

//draw grey background shape 
CGContextBeginPath(context); 

CGContextSetLineWidth(context, [lineWeight doubleValue]); 
//CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
CGContextSetRGBStrokeColor(context, .65, .65, .65, 1); 
CGContextSetAllowsAntialiasing(context, 1); 


//outer arc 
CGContextAddArc(context,            //context 
       widthMiddle,           //X-value for center point of arc 
       heightMiddle,           //Y-value for center point of arc 
       arcRadius,            //Radius of the arc 
       startAngle,     //start angle in radians 
       endAngle, //end angle in radians (2pi = full circle) 
       0);              //Clockwise? 1 = true 
//inner arc 
CGContextAddArc(context,            //context 
       widthMiddle,           //X-value for center point of arc 
       heightMiddle,           //Y-value for center point of arc 
       innerRadius,          //Radius of the arc 
       endAngle - interfaceAngle,         //start angle in radians 
       startAngle + interfaceAngle,             //end angle in radians (2pi = full circle) 
       1);              //Clockwise? 1 = true 
CGContextClosePath(context); 



//CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
CGContextSetRGBFillColor(context, .65, .65, .65, 1); 
//CGContextSetAlpha(context, .6); 
CGContextDrawPath(context, kCGPathFillStroke); 

答えて

1

CGContextClipは、現在のクリッピングパスと現在のパスの交差点にコンテキストのクリッピングパスを設定し、現在のパスをクリアします。 CGContextEOClipも同じですが、パス内の「穴」を扱う方法が異なります。異なるものは、パス自体が交差している場合、または複数の閉じたサブパスを含む場合にのみ重要です。

+0

Yep!それがそれでした。助けてくれてありがとう、(長い)遅れている確認については残念です。 – Amos

関連する問題