如何使用鼠标在超级视图内移动子视图(使用osx 10.6)? 我使用for循环以编程方式创建了五个NSimageView作为子视图。 如何选择和拖动每个图像视图
答案 0 :(得分:3)
简而言之,您希望使子视图拖动源,并使目标视图成为目标。这意味着实现类似NSDraggingSource和NSDraggingDestination协议的东西。
看一下Apple的Drag& amp;删除文档: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop
答案 1 :(得分:0)
最后我得到了答案
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (NSView *)hitTest:(NSPoint)aPoint
{
NSEnumerator *subviews = [[self subviews] objectEnumerator] ;
NSView *hitView ;
NSLog(@"hit");
fHitView = nil ;
while (hitView = [subviews nextObject]) {
NSRect frame = [hitView frame] ;
if (NSPointInRect(aPoint,frame))
if ([hitView isKindOfClass:[DraggableView class]] && ![(DraggableView *)hitView dragEnabled]) {
return hitView ;
}
else {
fHitView = hitView ;
fHitPoint = aPoint ;
fFrameWhenHit = [hitView frame] ;
return self ;
}
}
return nil ;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
if (fHitView != nil) {
NSPoint locationInWindow = [theEvent locationInWindow] ;
NSPoint locationInMySelf = [self convertPoint:locationInWindow fromView:[[self window] contentView]] ;
[fHitView setFrame:NSOffsetRect(fFrameWhenHit,locationInMySelf.x - fHitPoint.x, locationInMySelf.y - fHitPoint.y)] ;
[self setNeedsDisplay:YES] ;
}
}
插入NSview的子类并将NScustomView类的类名更改为子类名...