[英]Draggable NSTextField in Cocoa
我正在開發打印發票的應用程序。 我編寫以下代碼以使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];
}
每當使用委托函數調整窗口大小時,都必須重新定位文本字段。
- (void)windowDidResize:(NSNotification *)notification{
//Change the position of your textfield depending on the window size
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.