首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTableView draggingSession的坐标调整

NSTableView draggingSession的坐标调整
EN

Stack Overflow用户
提问于 2014-12-08 18:13:58
回答 1查看 736关注 0票数 2

大约两个星期以来,我一直在拼命工作,我查看了所有可用的苹果文档,以及数百个网站,寻找解决问题的方法。我正在实施:

代码语言:javascript
复制
-(void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes

我遇到的问题是,拖动文件的图标显示在不同的坐标系中,而且我似乎找不到一种方法使图标显示在拖动开始的screenPoint上。我已经在几个组合中研究了所有tableView转换*方法,但都没有成功。下面是我目前使用的代码。

代码语言:javascript
复制
-(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;
 }];}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-02 14:31:01

在彻底阅读了这些文档之后,我终于得到了它。

Docfor-enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock:说"forView:“参数是每个NSDraggingItem传递的坐标系统应该基于的视图。屏幕坐标系统为零。

令人困惑的是,我是通过在draggingFrame上登录draggingItem进行调试的,却找不出它是什么帧。好吧,不管里面是什么,如果您将0作为视图传递,您可以将拖放帧设置为基于屏幕的坐标,这与调用session.draggingLocation时得到的相同。

代码语言:javascript
复制
#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];
     }];
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27364231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档