允许单击并拖动视图来拖动窗口本身吗? [英] Allow click and dragging a view to drag the window itself?

查看:27
本文介绍了允许单击并拖动视图来拖动窗口本身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是带纹理的窗口,它的顶部有一个标签栏,就在标题栏的下方.

I'm using a textured window that has a tab bar along the top of it, just below the title bar.

我使用过 -setContentBorderThickness:forEdge: 使渐变看起来正确,并确保纸张从正确的位置滑出.

I've used -setContentBorderThickness:forEdge: on the window to make the gradient look right, and to make sure sheets slide out from the right position.

然而,不起作用的是拖动窗口.如果我单击并拖动实际上是标题栏的区域,它会起作用,但是由于标题栏渐变溢出到(可能/通常是空的)标签栏,真的点击太低很容易,感觉当您尝试拖动并意识到窗口没有移动时,真的很令人沮丧.

What's not working however, is dragging the window around. It works if I click and drag the area that's actually the title bar, but since the title bar gradient spills into a (potentially/often empty) tab bar, it's really easy to click too low and it feels really frustrating when you try to drag and realise the window is not moving.

我注意到 NSToolbar 虽然在标题栏下方占据大致相同的空间,但允许在光标悬停在其上方时拖动窗口.如何实现这一点?

I notice NSToolbar, while occupying roughly the same amount of space below the title bar, allows the window to be dragged around when the cursor is over it. How does one implement this?

谢谢.

推荐答案

我在这里找到了这个:

-(void)mouseDown:(NSEvent *)theEvent {    
    NSRect  windowFrame = [[self window] frame];

    initialLocation = [NSEvent mouseLocation];

    initialLocation.x -= windowFrame.origin.x;
    initialLocation.y -= windowFrame.origin.y;
}

- (void)mouseDragged:(NSEvent *)theEvent {
    NSPoint currentLocation;
    NSPoint newOrigin;

    NSRect  screenFrame = [[NSScreen mainScreen] frame];
    NSRect  windowFrame = [self frame];

    currentLocation = [NSEvent mouseLocation];
    newOrigin.x = currentLocation.x - initialLocation.x;
    newOrigin.y = currentLocation.y - initialLocation.y;

    // Don't let window get dragged up under the menu bar
    if( (newOrigin.y+windowFrame.size.height) > (screenFrame.origin.y+screenFrame.size.height) ){
        newOrigin.y=screenFrame.origin.y + (screenFrame.size.height-windowFrame.size.height);
    }

    //go ahead and move the window to the new location
    [[self window] setFrameOrigin:newOrigin];
}

它工作得很好,虽然我不是 100% 确定我做对了.到目前为止,我发现了一个错误,那就是如果拖动从子视图(选项卡本身)内开始,然后进入超级视图(选项卡栏).窗户跳来跳去.一些 -hitTest: 魔法,或者甚至可能只是使 mouseUp 上的 initialLocation 无效应该可以解决这个问题.

It works fine, though I'm not 100% sure I'm doing it correctly. There's one bug I've found so far, and that's if the drag begins inside a subview (a tab itself) and then enters the superview (the tab bar). The window jumps around. Some -hitTest: magic, or possibly even just invalidating initialLocation on mouseUp should probably fix that.

这篇关于允许单击并拖动视图来拖动窗口本身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆