大约两个星期以来,我一直在拼命工作,我查看了所有可用的苹果文档,以及数百个网站,寻找解决问题的方法。我正在实施:
-(void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes我遇到的问题是,拖动文件的图标显示在不同的坐标系中,而且我似乎找不到一种方法使图标显示在拖动开始的screenPoint上。我已经在几个组合中研究了所有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;
}];}发布于 2016-03-02 14:31:01
在彻底阅读了这些文档之后,我终于得到了它。
Docfor-enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock:说"forView:“参数是每个NSDraggingItem传递的坐标系统应该基于的视图。屏幕坐标系统为零。
令人困惑的是,我是通过在draggingFrame上登录draggingItem进行调试的,却找不出它是什么帧。好吧,不管里面是什么,如果您将0作为视图传递,您可以将拖放帧设置为基于屏幕的坐标,这与调用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];
}];
}https://stackoverflow.com/questions/27364231
复制相似问题