私は初心者の ios プログラマーです。私のプロジェクトの 1 つで、円の異なる部分が異なる色で塗りつぶされる円を描く必要があります。円を描くことはできますが、異なる部分を特定できません。円の色を変えて、別の色で塗りつぶします。何を描きたいかを明確にするためのスクリーンショットを次に示します。
助けていただければ幸いです。
私は初心者の ios プログラマーです。私のプロジェクトの 1 つで、円の異なる部分が異なる色で塗りつぶされる円を描く必要があります。円を描くことはできますが、異なる部分を特定できません。円の色を変えて、別の色で塗りつぶします。何を描きたいかを明確にするためのスクリーンショットを次に示します。
助けていただければ幸いです。
半径、中心、角度を指定できるUIBezierPath
メソッドを持つ which を使用できます。addArcWithCenter:radius:startAngle:endAngle:clockwise:
コードは次のようになり、円の 4 分の 1 を緑色で描画します。
CGPoint center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f, CGRectGetHeight(self.bounds) / 2.f);
CGFloat radius = center.x - 10.f;
UIBezierPath *portionPath = [UIBezierPath bezierPath];
[portionPath moveToPoint:center];
[portionPath addArcWithCenter:center radius:radius startAngle:0.f endAngle:M_PI_2 clockwise:YES];
[portionPath closePath];
[[UIColor greenColor] setFill];
[portionPath fill];
UIBezierPath *portionPath1 = [UIBezierPath bezierPath];
[portionPath1 moveToPoint:center];
[portionPath1 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
[portionPath1 closePath];
[[UIColor blueColor] setFill];
[portionPath1 fill];
もちろん、CorePlotのようなライブラリを使用することも検討できます。
三角関数の簡単な問題です。パイの部分に必要な角度 (パイ スライス パーセンテージ タイム 360 度) を計算してから、中心に移動します。適切な角度で円の端に線を引き、必要な弧の角度でパイ スライスの次の側に弧を描き、中心に戻ります。
または、 CorePlotを使用して円グラフを作成することもできます。