1

如何使用鼠标在超级视图中移动子视图(使用 osx 10.6)?
我已经使用 for 循环以编程方式创建了五个 NSimageView 作为子视图。如何选择和拖动每个图像视图

4

2 回答 2

3

简而言之,您想让您的子视图拖动源,并使您的目标视图成为目的地。这意味着实现类似 NSDraggingSource 和 NSDraggingDestination 协议。

查看 Apple 的拖放文档: https ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop

于 2013-04-05T20:10:52.613 回答
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 类的类名更改为子类名...

于 2013-04-10T10:48:25.873 回答