請求書を印刷するアプリケーションを開発しています。NSTextFieldsをドラッグ可能にして、Invoice-fieldsと一致させるために、次のコードを記述します。動作しますが、ウィンドウのサイズを変更すると、NSTextFieldが初期位置に戻ります。私に何ができる?
@interface DragTextField : NSTextField <NSWindowDelegate>
@property (readwrite) NSPoint location;
@end
@implementation DragTextField
@synthesize location;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
}
- (BOOL) acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent
{
location = [theEvent locationInWindow];
self.location = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint newDragLocation = [[self superview] convertPoint:[theEvent locationInWindow] fromView:nil];
NSPoint thisOrigin = [self frame].origin;
thisOrigin.x += (-self.location.x + newDragLocation.x);
thisOrigin.y += (-self.location.y + newDragLocation.y);
[self setFrameOrigin:thisOrigin];
self.location = newDragLocation;
}
- (void)mouseUp:(NSEvent *)theEvent {
[self setNeedsDisplay:YES];
}