我有一个名为dragBox的NSBox子类。我希望能够在画布上拖动它。代码如下:
-(void) awakeFromNib
{
[[self superview] registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
-(void) mouseDown:(NSEvent *)theEvent
{
[self dragImage:[[NSImage alloc] initWithContentsOfFile:@"/Users/bruce/Desktop/Untitled-1.png"] at:NSMakePoint(32, 32) offset:NSMakeSize(0,0) event:theEvent pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] source:self slideBack:YES];
}
-(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender // validate
{
NSLog(@"Updated");
return [sender draggingSourceOperationMask];
}
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
NSLog(@"Drag Entered");
return [sender draggingSourceOperationMask];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSLog(@"Move Box");
[self setFrameOrigin:[sender draggingLocation]];
return YES;
}
-(BOOL) prepareForDragOperation:(id<NSDraggingInfo>)sender
{NSLog(@"Prepared");
return YES;
}为什么不调用dragEntered?我已经尝试使用所有的pboard类型和类似的。似乎什么都不起作用。我还更改了registerForDraggedTypes,使其只在self视图中工作。该框是画布的子视图。
布鲁斯
发布于 2013-11-10 06:52:21
我发现将registerForDragTypes调用放在awakeFromNib是错误的,因为我是以编程方式添加视图(即,不是通过Nib添加视图)。我必须将调用放入initWithFrame:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self registerForDraggedTypes: [NSArray arrayWithObjects:NSTIFFPboardType,NSFilenamesPboardType,nil]];
}
return self;
}发布于 2013-06-14 18:10:54
布鲁斯
您的代码需要按以下方式进行更改。我认为应该为拖动类型注册视图,以使方法draggingEntered被调用。
@interface NSModifiedBox : NSBox
@end
@implementation NSModifiedBox
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
[self registerForDraggedTypes:
[NSArray arrayWithObjects:NSTIFFPboardType,NSFilenamesPboardType,nil]];
[super drawRect:dirtyRect];
}
- (NSDragOperation)draggingEntered:(id )sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric)
{
return NSDragOperationGeneric;
} // end if
// not a drag we can use
return NSDragOperationNone;
}
- (BOOL)prepareForDragOperation:(id )sender
{
return YES;
}
@end现在,将NSBox拖放到Xib上,并修改NSBox的类以将断点拖放到方法"draggingEntered".
希望我的回答能对你有所帮助:)
https://stackoverflow.com/questions/16814846
复制相似问题