NSImage在拖动操作之间“保留”

时间:2013-01-11 12:27:43

标签: macos cocoa drag-and-drop nstableview

您好我实施了:

- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns event:(NSEvent *)dragEvent offset:(NSPointPointer)dragImageOffset
{
    NSImage *dragImage = [NSImage imageNamed:@"Drag-Icon.png"];
    NSInteger numberOfItems = dragRows.count;
    NSAttributedString *numbers = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%lu",numberOfItems] attributes:attributes];
    [dragImage lockFocus];
    NSRect numbersSurroundRect = NSMakeRect(dragImage.size.width - (numbers.size.width + 15) - strokeWidth - 5, strokeWidth, numbers.size.width + 15, boxHeight);
    NSBezierPath *circle = [NSBezierPath bezierPathWithRoundedRect:numbersSurroundRect xRadius:9.0 yRadius:9.0];
    [[NSColor redColor] set];
    [circle fill];
    [[NSColor whiteColor] set];
    [circle setLineWidth:strokeWidth];
    [circle stroke];
    numbersSurroundRect.origin.y += ((numbersSurroundRect.size.height - numbers.size.height) + 1.75);
    [numbers drawInRect:numbersSurroundRect];
    [dragImage unlockFocus];
    return dragImage;
}

在我的NSTableView子类中,当我在表格中拖动多行时,我得到:

enter image description here

然后,当我只拖动一两行时,我得到:

enter image description here

似乎返回旧行计数,然后在其上方绘制新计数....

请问有人对此有所了解吗?是在我的项目中使用ARC而不知何故

的结果

1 个答案:

答案 0 :(得分:1)

您正在直接绘制到原始图像上。您应该在绘制之前复制一份,以便下次可以在新的副本上绘制。