使用NSTableView自定义拖动图像作为拖动源 [英] Custom drag image with NSTableView as drag source

查看:291
本文介绍了使用NSTableView自定义拖动图像作为拖动源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果tableView是拖动源,是否需要将NSTableView或NSTableCellView子类化以创建自定义拖动图像?



如果不是,我是什么魔法方法缺少这样做?我似乎找不到任何东西。



NSTableCellView 子类可以(稍微神秘地)覆盖:



@property(保留,只读)NSArray * draggingImageComponents
NSDraggingImageComponent 数组,将被合并在一起的实例谁知道他们以什么样的方式合成...))



NSTableView 本身有



(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns事件:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset



但这些确实是子类化选项。




(10.8+是目标)

解决方案

看起来好像 NSTableViewCell

  @property(保留,只读)NSArray * draggingImageComponents 
pre>

不会自动调用,但必须显式引用基于视图的表视图。可以覆盖 -draggingImageComponents 以提供用于复合拖动图像的组件。

   - (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)会话willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes 
{
//配置拖动图像
//我们只拖动一个项目 - 需要更改来处理多个拖动项目
BPPopupButtonTableCellView * cellView = [self.columnsTableView viewAtColumn:0 row:rowIndexes.firstIndex makeIfNecessary:NO];
if(cellView){

[session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
forView:tableView
类:[NSArray arrayWithObject:[NSPasteboardItem class]]
searchOptions: nil
usingBlock:^(NSDraggingItem * draggingItem,NSInteger idx,BOOL * stop)
{
//我们可以直接设置拖动图像
// [draggingItem setDraggingFrame:NSMakeRect 0,0,myWidth,myHeight)内容:myFunkyDragImage];

//默认情况下,tableview将抓取整个表单元格视图边界作为其图像。
//我们可以覆盖NSTableCellView -draggingImageComponents
//其默认值仅包括图像和文本字段
draggingItem.imageComponentsProvider = ^ NSArray *(void){return cellView.draggingImageComponents;};
}];
}
}


Is it necessary to subclass NSTableView or NSTableCellView to create a custom drag image when the tableView is the drag source?

If not, what is the magic method I am missing to do this? I cannot seem to find anything solid.

NSTableCellView subclasses have can (slightly mysteriously) override:

@property(retain, readonly) NSArray *draggingImageComponents (array of NSDraggingImageComponent instances that will get composited together (who knows in what fashion they get composited...))

NSTableView itself has

- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset

But these are indeed subclassing options it seems.

Anybody know another technique here? (10.8+ is target )

解决方案

It looks as if NSTableViewCell

@property(retain, readonly) NSArray *draggingImageComponents

does not get called automatically but must be explicitly referenced for view based table views. -draggingImageComponents can be overridden to supply the components used to composite the drag image.

- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes
{
    // configure the drag image
    // we are only dragging one item - changes will be required to handle multiple drag items
    BPPopupButtonTableCellView *cellView = [self.columnsTableView viewAtColumn:0 row:rowIndexes.firstIndex makeIfNecessary:NO];
    if (cellView) {

        [session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
                                           forView:tableView
                                           classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                     searchOptions:nil
                                        usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop)
         {
             // we can set the drag image directly
             //[draggingItem setDraggingFrame:NSMakeRect(0, 0, myWidth, myHeight) contents:myFunkyDragImage];

             // the tableview will grab the entire table cell view bounds as its image by default.
             // we can override NSTableCellView -draggingImageComponents
             // which defaults to only including the image and text fields
             draggingItem.imageComponentsProvider = ^NSArray*(void) { return cellView.draggingImageComponents;};
         }];
    }
}

这篇关于使用NSTableView自定义拖动图像作为拖动源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆