ベジエ曲線(UIBezierPath)
CoreGraphicsでは, ベジエ曲線を描画するためのクラスとして, UIBezierPathというのを用意してくれています。
UIBezierPathには, stroke, fillというメソッドがありそれぞれ線, 中身の描画を表します。
サンプルコード
- (void)drawRect:(CGRect)rect { // Drawing code CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0); UIBezierPath *bezier = [UIBezierPath bezierPath]; bezier.lineWidth = 5; [[UIColor whiteColor] setStroke]; // Color [bezier moveToPoint:CGPointMake(100, 0)]; [bezier addLineToPoint:CGPointMake(200, 40)]; [bezier addLineToPoint:CGPointMake(160, 140)]; [bezier addLineToPoint:CGPointMake(40, 140)]; [bezier addLineToPoint:CGPointMake(0, 40)]; [bezier closePath]; //rendering [bezier stroke]; [bezier fill]; }
ストローク部分を白で, 中身をGreenで描いてみました。基本はmoveToPointで始点を動かし, addLineToPointで描画するポイントを移動していく所ですね。
最後にパスを閉じて, 描画します。