CGPathRef
これまではパスを作るのに直接 CGContextを操作していたが、角丸矩形(RoundRect)のパスは頻繁に使われるので CGPathRef を使うことにする。
CGPath Reference
使い方は次の通り
1. CGPathCreateMutable() を使い CGMutablePathRef を作成する(CGMutablePathはCGPathのサブクラス)
2. CGPath系の関数を使い、CGMutablePath パスを追加する
3. CGContextAddPath() でコンテキストへパスを追加する
4. 利用が終わった後、CGContextAddPath で開放
今回は角丸矩形のパスを予め作成しておき、これを必要な箇所で使う方法に切り替えた。こんな感じ。
- (void)createRoundRectPath
{
roundRectPath_ = CGPathCreateMutable();
// Top Left
CGPathMoveToPoint(roundRectPath_, NULL,
contentFrame_.origin.x, contentFrame_.origin.y + CORNER_RADIUS);
CGPathAddArcToPoint(roundRectPath_, NULL,
contentFrame_.origin.x,
contentFrame_.origin.y,
contentFrame_.origin.x + CORNER_RADIUS,
contentFrame_.origin.y,
CORNER_RADIUS);
// Top right
CGPathAddArcToPoint(roundRectPath_, NULL,
contentFrame_.origin.x + contentFrame_.size.width,
contentFrame_.origin.y,
contentFrame_.origin.x + contentFrame_.size.width,
contentFrame_.origin.x + CORNER_RADIUS,
CORNER_RADIUS);
// Bottom right
CGPathAddArcToPoint(roundRectPath_, NULL,
contentFrame_.origin.x + contentFrame_.size.width,
contentFrame_.origin.y + contentFrame_.size.height,
contentFrame_.origin.x + contentFrame_.size.width - CORNER_RADIUS,
contentFrame_.origin.y + contentFrame_.size.height,
CORNER_RADIUS);
// Bottom left
CGPathAddArcToPoint(roundRectPath_, NULL,
contentFrame_.origin.x,
contentFrame_.origin.y + contentFrame_.size.height,
contentFrame_.origin.x,
contentFrame_.origin.y,
CORNER_RADIUS);
CGPathCloseSubpath(roundRectPath_);
}



Responses
Leave a Response