0

问候,

我对在NSCollectionView子类中应该如何以及何时调用drawRect感到困惑。

我实现了拖放操作,以便在集合中移动NSCollectionViewItems,并希望绘制一个视觉指示来指示拖放的结束位置。

子类在拖动会话期间不调用 drawRect。(它在滚动期间发生)

这是预期的操作吗?欢迎任何有关如何正确实施此行为的提示。

以下代码的完整 xcode 项目可在以下位置获得:http: //babouk.ovh.org/dload/MyCollectionView.zip

此致

更新: 示例代码错误。对 registerForDraggedTypes 的调用使集合按预期触发 drawRect。

代码示例:

@interface CollectionViewAppDelegate : NSObject <NSApplicationDelegate> 
{
    NSWindow            *window;
    NSMutableArray      *collectionContent;
}
/* properties declaration */
/* KVC compliance declarations */
@end

@interface MyCollectionView : NSCollectionView
@end

@interface ItemModel
{
    NSString *name;
}
@property (copy)  NSString *name;
@end

@implementation MyCollectionView
- (void)awakeFromNib {
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSStringPboardType, nil]];
}

- (void)drawRect:(NSRect)rect {
    NSLog(@"DrawRect");
}

- (void)mouseDragged:(NSEvent *)aEvent {
    NSPoint     localPoint = [self convertPoint:[aEvent locationInWindow] fromView:nil];
    [self dragImage:[NSImage imageNamed:@"Move.png"] 
                 at:localPoint
             offset:NSZeroSize
              event:aEvent 
         pasteboard:nil 
             source:self 
          slideBack:NO];
}

- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender {
    return YES;
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    return NSDragOperationEvery;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender {
    [self setNeedsDisplay:YES];
    return NSDragOperationEvery;
}

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal {    
    return NSDragOperationEvery;
}

- (void)mouseDown:(NSEvent *)theEvent {
}

@end

@implementation CollectionViewAppDelegate

@synthesize window, collectionContent, collectionView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSMutableArray *data = [[NSMutableArray alloc] init];
  /* Fill in data */
    [self setCollectionContent:data];
    [data release];
}

/* dealloc */

/* KVC implementation */
@end

@implementation ItemModel
@synthesize name;
/* dealloc */
@end
4

1 回答 1

1

我认为您不能成功地继承NSCollectionView的绘图例程。在内部,集合视图布局在核心动画层中,因此-drawRect:不用于渲染其内容。

于 2010-04-09T01:13:24.797 回答