0

我正在开发一个打印发票的应用程序。我编写以下代码使 NSTextFields 可拖动并与发票字段匹配。它可以工作,但是当调整窗口大小时,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];
}
4

1 回答 1

-1

每当使用委托函数调整窗口大小时,您都必须重新定位文本字段。

- (void)windowDidResize:(NSNotification *)notification{ 
//Change the position of your textfield depending on the window size
}
于 2012-11-02T13:53:00.703 回答