【问题标题】:how to move subviews inside a super view如何在超级视图中移动子视图
【发布时间】:2013-04-03 13:07:53
【问题描述】:

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

【问题讨论】:

    标签: macos cocoa osx-snow-leopard


    【解决方案1】:

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

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

    【讨论】:

    • DragItemAround 如何添加控件,例如 NSButton 等,而不是可拖动的矩形。
    • 我已经在框架中填充了一个图像,是否可以创建另一个框架 [dragItemAround](developer.apple.com/library/mac/#samplecode/DragItemAround/…) 编码
    • NSDraggingSourceNSDraggingDestination 仅适用于 OSX 10.7
    • 让一个对象实现拖动源协议而另一个对象实现拖动目标(或放置目标)的一般模式非常可靠,并且被许多操作系统使用。在 iOS 上,您只能靠自己,但请查看 Apple 在 Mac 上使用的协议,您可以为自己的应用程序创建类似的东西。您还可以找到为您实现此功能的工具 - 例如,请查看 dragkit.org
    【解决方案2】:

    终于找到答案了

        - (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类的类名改成子类名...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多