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

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

问题描述

我使用的是带纹理的窗口,其顶部有一个标签栏,位于标题栏下方。



我使用了 -setContentBorderThickness:forEdge:在窗口上使渐变显示正确,并确保页面从正确的位置滑出。



什么不工作,但是,是拖动窗口周围。它的工作原理,如果我点击并拖动实际上是标题栏的区域,但由于标题栏梯度溢出(可能/通常是空的)标签栏,这是真的容易点击太低,感觉真的令人沮丧,当你试图拖动和实现窗口不移动。



我注意到NSToolbar,虽然占据大致相同的空间下面的标题栏,允许窗口当光标在其上时被拖动。



解决方案

我发现这个此处

   - (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;

//不要让窗口在菜单栏上拖动
if((newOrigin.y + windowFrame.size.height)>(screenFrame.origin.y + screenFrame。 size.height)){
newOrigin.y = screenFrame.origin.y +(screenFrame.size.height-windowFrame.size.height);
}

//继续将窗口移动到新位置
[[self window] setFrameOrigin:newOrigin];
}

这很好,但我不是100%它正确。有一个bug,我发现到目前为止,这是如果拖动开始在一个子视图(一个选项卡本身),然后进入超级视图(选项卡栏)。窗口跳动。一些-hitTest:魔术,或者甚至只是使mouseUp上的initialLocation无效可能会解决这个问题。


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

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.

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?

Thanks.

解决方案

I found this here:

-(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];
}

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天全站免登陆