让MAAttachedWindow可以调整大小而不会反弹?

时间:2011-05-01 02:33:57

标签: cocoa resizable

要使MAAttachedWindow可调整大小,我使用here中的代码:

- (void)mouseDown:(NSEvent *)event {
NSPoint pointInView = [event locationInWindow];

BOOL resize = YES;
if (NSPointInRect(pointInView, [self resizeRect]))
{
    resize = YES;
}

NSWindow *window = self;
NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];
NSRect originalFrame = [window frame];

while (YES)
{
    //
    // Lock focus and take all the dragged and mouse up events until we
    // receive a mouse up.
    //
    NSEvent *newEvent = [window
                         nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

    if ([newEvent type] == NSLeftMouseUp)
    {
        break;
    }

    //
    // Work out how much the mouse has moved
    //
    NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];
    NSPoint delta = NSMakePoint(
                                newMouseLocation.x - originalMouseLocation.x,
                                newMouseLocation.y - originalMouseLocation.y);

    NSRect newFrame = originalFrame;

    if (!resize)
    {
        //
        // Alter the frame for a drag
        //
        newFrame.origin.x += delta.x;
        newFrame.origin.y += delta.y;
    }
    else
    {
        //
        // Alter the frame for a resize
        //
        //          newFrame.size.width += delta.x;
        newFrame.size.width += delta.x*2; // customize
        newFrame.size.height -= delta.y;
        newFrame.origin.y += delta.y;

        //
        // Constrain to the window's min and max size
        //
        NSRect newContentRect = [window contentRectForFrameRect:newFrame];
        NSSize maxSize = [window maxSize];
        NSSize minSize = [window minSize];
        if (newContentRect.size.width > maxSize.width)
        {
            newFrame.size.width -= newContentRect.size.width - maxSize.width;
        }
        else if (newContentRect.size.width < minSize.width)
        {
            newFrame.size.width += minSize.width - newContentRect.size.width;
        }
        if (newContentRect.size.height > maxSize.height)
        {
            newFrame.size.height -= newContentRect.size.height - maxSize.height;
            newFrame.origin.y += newContentRect.size.height - maxSize.height;
        }
        else if (newContentRect.size.height < minSize.height)
        {
            newFrame.size.height += minSize.height - newContentRect.size.height;
            newFrame.origin.y -= minSize.height - newContentRect.size.height;
        }
    }




    [window setFrame:newFrame display:NO animate:NO];
    }

}

当我拖动窗口时,我可以调整它的大小。但是偶尔会有一点反弹。 您可以下载demo项目。 运行该项目,然后单击状态菜单上的图标以打开该窗口。尝试前后伸展,这样您就可以轻松地重现它。 希望有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我检查了你的代码,似乎偶尔NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];将返回原始鼠标位置而不是新鼠标位置。

我不确定为什么会失败,但不管这不是最佳方式。您应该不断更新该位置,而只是将窗口移动鼠标在当前跟踪迭代中移动的数量

,而不是从原始鼠标位置计算新帧。

这是一个清理过的缩短版本,似乎可以消除这个错误。

- (void) mouseDown:(NSEvent *)event
{
    NSWindow *window = self;
    NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];


while (YES)
{

    NSEvent *newEvent = [window
                         nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

    if ([newEvent type] == NSLeftMouseUp)
    {
        break;
    }

    NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];

    NSPoint delta = NSMakePoint(
                                roundf(newMouseLocation.x - originalMouseLocation.x),
                                roundf(newMouseLocation.y - originalMouseLocation.y));

    NSRect newFrame = [window frame];
    newFrame.size.width += delta.x*2; // customize
    newFrame.size.height -= delta.y;
    newFrame.origin.y += delta.y;

    [window setFrame:newFrame display:YES animate:NO];
    originalMouseLocation = newMouseLocation; // <-- added this

}

}