Cocoa入門
なんとなくMac OS Xの開発環境であるObjective-c Cocoaで開発してみる気になったので、簡単なアプリを作成。
環境の構築は、付属のDVDに入っていたXcodeをインストールするだけでOK。
開発の主な流れとしてはInterfaceBuilderでUIを作って、それに対してコーディングを行う。
キャンバスのような物にごりごり書くプログラムなら、NSViewクラスを継承したクラスを作成し、 それをInterfaceBuilder上でUIの上に配置してNSViewクラスのdrawRect関数をオーバーライドすると その中に描画することができる。
#import "DotView.h"
@implementation DotView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
center = CGRectMake(20, 20, 20, 20);
}
return self;
}
- (void)drawRect:(NSRect)rect {
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextFillRect(context, NSRectToCGRect([self bounds]));
CGContextSetRGBFillColor(context, 0, 0, 1, .5);
CGContextFillRect(context, center);
}
- (void)mouseDown:(NSEvent *)theEvent{
center.origin = NSPointToCGPoint([theEvent locationInWindow]);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent{
[self mouseDown:theEvent];
}
@end
開発に必要なドキュメントはXcodeのヘルプの製品ドキュメントより参照可能。
そのほか開発に必要な物は全てApple Developer Connectionにある。なお、WWDCのCocoaFoundationに関する1時間くらいのデモビデオが非常にわかりやすいのでオススメ
| 固定リンク


コメント