ご挨拶、
私は、drawRectがNSCollectionViewサブクラスでいつどのように呼び出されるのかについて戸惑っています。
コレクション内で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