【发布时间】:2013-04-03 13:07:53
【问题描述】:
如何使用鼠标在超级视图中移动子视图(使用 osx 10.6)?
我已经使用 for 循环以编程方式创建了五个 NSimageView 作为子视图。
如何选择和拖动每个图像视图
【问题讨论】:
标签: macos cocoa osx-snow-leopard
如何使用鼠标在超级视图中移动子视图(使用 osx 10.6)?
我已经使用 for 循环以编程方式创建了五个 NSimageView 作为子视图。
如何选择和拖动每个图像视图
【问题讨论】:
标签: macos cocoa osx-snow-leopard
简而言之,您希望让子视图拖动源,并使目标视图成为目的地。这意味着实现类似 NSDraggingSource 和 NSDraggingDestination 协议。
查看 Apple 的拖放文档: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop
【讨论】:
终于找到答案了
- (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类的类名改成子类名...
【讨论】: