Interface Builderのおかげで、画像を表示したり、ボタンや文字を表示したりするのは大変簡単な、iPhone SDK。
しかし、線や丸など、ビューに直接書くようなものは意外と面倒なので、メモ。
- Skeltonから"Window-based Application"を選択して、プロジェクトを作成
- "グループとファイル"で"Classes"を選択しておいた状態で、"ファイル"->"新規ファイル"を選択
- "Cocoa Touch Class"、"Objective-C class"を選択し、"Subclass of"で"UIView"を選択して、"次へ"
- 適当にオブジェクト名=クラス名(例えば"myAppView")をつけて、"OK"
- "MainWindow.lib"をダブル・クリックして、Interface Builderで開く
- "Library"からViewオブジェクトを選択して、Windowに貼付ける
- "Attribute Inspector"で"Indenty"タブを開く、Apple-4でショートカット・アクセスできる
- "Class"で、先ほど作ったクラス名(例通りなら"myAppView")を選択
- 保存して、Interface Builderを終了
- Xcodeに戻り、先ほど作ったクラスのモジュールファイル(例通りなら"myAppView.m")を開く
- "drawRect"メソッドがSkeltonではコメント・アウトされているはずなので、コメントを外し、例えば以下の様に実装
以上、あとは、ビルドして実行すればよい。
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
//
CGContextSetLineWidth(context, 2.0);
UIColor *color= [UIColor blueColor];
CGContextSetStrokeColorWithColor(context, color.CGColor);
//
NSInteger angle;
float pos_x, pos_y;
CGContextMoveToPoint(context, 0.0, 100.0 );
for ( angle = 0; angle < 360; angle++ )
{
pos_x = 320.0 * ( (float) angle )/ 360.0;
pos_y = 100.0 - 100.0 * ( (float) sin ( 2.0 * M_PI * angle / 360.0 ) );
CGContextAddLineToPoint(context, pos_x , pos_y );
}
CGContextStrokePath(context);
//
[color release];
}