我已经把头撞到墙上大约两周了,我查看了所有可用的Apple文档以及数百个网站,寻找解决我问题的提示。我正在实施:
-(void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes
我遇到的问题是拖动文件的图标显示在不同的坐标系中,我似乎找不到一种方法使图标显示在拖动开始的屏幕上。我已经研究了几种组合中的所有 [tableView 转换* ] 方法,但没有成功。以下是我当前使用的代码。
-(void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes {
[session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
forView:tableView
classes:[NSArray arrayWithObjects:[NSPasteboardItem class], nil]
searchOptions:nil
usingBlock:^(NSDraggingItem *draggingItem, NSInteger index, BOOL *stop)
{
NSMutableArray *videos = [NSMutableArray array];
for (Video* video in [_arrayController selectedObjects]) {
[videos addObject:video.url];
}
NSImage *draggedImage = [[NSWorkspace sharedWorkspace]iconForFiles:videos];
NSPoint mouseLocInView = [tableView convertPoint:[tableView.window convertRectFromScreen:NSMakeRect(screenPoint.x,screenPoint.y, 0, 0)].origin fromView:nil];
NSLog(@"Mouse location in view: X: %f, Y: %f",mouseLocInView.x, mouseLocInView.y);
NSLog(@"Screen point: X: %f, Y:%f", screenPoint.x, screenPoint.y);
NSRect rect = NSMakeRect(0, 0, 50, 50);
rect.origin =draggingItem.draggingFrame.origin;
[draggingItem setDraggingFrame:rect contents:draggedImage];
session.draggingFormation = NSDraggingFormationDefault;
}];}
在仔细阅读文档后,我终于明白了。
的文档- 枚举拖动项目与选项:forView:类:搜索选项:usingBlock:表示"forView:"参数是每个传递的NSDraggingItem的坐标系应基于的视图。屏幕坐标系的传递 nil。
非常令人困惑的是,我正在通过在拖动项目上记录拖动框架来进行调试,但无法弄清楚它是什么帧。好吧,里面有什么并不重要,如果您将 nil 作为视图传递,您可以将拖动框架设置为基于屏幕的坐标,就像调用 session.draggingLocation 时一样。
#define DRAG_IMAGE_WIDTH 48
#define DRAG_IMAGE_HEIGHT 48
- (void)tableView:(NSTableView *)tableView
draggingSession:(NSDraggingSession *)session
willBeginAtPoint:(NSPoint)screenPoint
forRowIndexes:(NSIndexSet *)rowIndexes {
NSImage *image = [NSImage imageNamed:@"tunnelfile"];
[session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
forView:nil
classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
searchOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSPasteboardURLReadingFileURLsOnlyKey ]
usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop)
{
[draggingItem setDraggingFrame:NSMakeRect(session.draggingLocation.x-20,
session.draggingLocation.y-DRAG_IMAGE_HEIGHT+20,
DRAG_IMAGE_WIDTH,
DRAG_IMAGE_HEIGHT)
contents:image];
}];
}