【问题标题】:Draggable NSTextField in CocoaCocoa 中的可拖动 NSTextField
【发布时间】:2014-03-28 08:04:00
【问题描述】:

我正在开发一个打印发票的应用程序。我编写以下代码使 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];
}

【问题讨论】:

  • 是否有任何其他代码与文本字段的框架交互?如果是这样,请发布。

标签: cocoa nstextfield


【解决方案1】:

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

- (void)windowDidResize:(NSNotification *)notification{ 
//Change the position of your textfield depending on the window size
}

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多