- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSScreen* main_screen = [NSScreen mainScreen];
NSRect fullscreen_frame = [main_screen frame];
[window setFrame:fullscreen_frame display:YES];
[window setStyleMask:NSResizableWindowMask];
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
[window makeKeyAndOrderFront:self];
NSRect rect=NSMakeRect(100, 100, 640, 480);
MovableView *view=[[MovableView alloc] initWithFrame:rect];
[[window contentView] addSubview:view];
}
今回はMovableViewというNSViewのサブクラスを動的に生成してみた。このViewをマウスのドラッグで動かしたり、リサイズしたりする実験。
本日はドラッグでViewだけ動かしておしまい。
簡単に考えると、マウスのドラッグの移動量を現在のFrameのrectに足して、改めて[self setFrame:rect]とかやればいいような気がするわけだが、これがなぜかうまくいかない。rectのoriginが想定よりも倍くらい増えるんだなあ。なぜだろう。
マウスドラッグを追いかける方法はデベロッパドキュメントには3通り載っていて、xcatsan師匠はmouseDownでloopを回す方法をとっているけれど、今回は最初の例、mouseDownでドラッグ中のflagを立て、mouseDraggedで実際の処理をしてみた。
-(void)mouseDown:(NSEvent *)event{
NSLog(@"mouseDown");
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
if (NSPointInRect(p,[self frame])){
windowDrag=YES;
}
}
-(void)mouseDragged:(NSEvent *)theEvent{
NSLog(@"drag!!");
if(windowDrag){
NSRect rect=[self frame];
[self translateOriginToPoint:NSMakePoint(theEvent.deltaX, -theEvent.deltaY)];
[self setFrameSize:NSMakeSize(rect.size.width+theEvent.deltaX, rect.size.height-theEvent.deltaY)];
[self setNeedsDisplay:YES];
NSLog(@"df:%f dy %f",theEvent.deltaX,theEvent.deltaY);
}
}
とりあえず以上で、View内でマウスをクリック→ドラッグしたらViewが動く、という処理が実現できている。[self translateOriginToPoint:NSMakePoint(theEvent.deltaX, -theEvent.deltaY)];としないと、なぜoriginが想定したように動かないのかまだ理解出来ていない。結果オーライなコードなので自分でもかなり信用度が低い。(^^;)明日は休みなのでもう少し実験できるかな。
しかし在宅仕事の必要があるのでどの程度時間を割けるか・・・。
0 件のコメント:
コメントを投稿