【问题标题】:How to drag-move NSWindow with a NSImageView on its contentView?如何在其 contentView 上使用 NSImageView 拖动移动 NSWindow?
【发布时间】:2013-11-19 23:44:16
【问题描述】:

我正在创建一个类似于 iTunes 迷你窗口的迷你窗口:

它是可拖动的,并且上面有背景图片。

然后我创建了一个NSWindow 的子类并覆盖了它的initWithContentRect:styleMask:backing:defer: 方法,并为其添加了一个NSImageView:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
    contentRect.size = CGSizeMake(287, 287);
    self = [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
    if (self)
    {
        [self setMovableByWindowBackground:YES];
        [self setOpaque:NO];
        [self setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];

        NSImageView *imageView = [[NSImageView alloc] initWithFrame:(CGRect){CGPointZero, contentRect.size}];
        imageView.image = [NSImage imageNamed:@"MiniWindow.png"];
        [self.contentView addSubview:imageView];
    }
    return self;
}

但是在我将NSImageView 添加到窗口的 contentView 后,窗口变得不可拖动。

如何让窗口再次变得可拖动?

最好的问候

【问题讨论】:

  • 我认为您正在尝试拖动您的图像视图。

标签: macos cocoa nswindow


【解决方案1】:

创建NSImageView 的子类并覆盖mouseDownCanMoveWindow 方法:

- (BOOL)mouseDownCanMoveWindow
{
    return YES;
}

【讨论】:

  • 这似乎是一个正确且有用的答案(对我有用 - 谢谢!) - 为什么没有这样标记?
猜你喜欢
  • 2017-01-11
  • 2015-09-06
  • 2017-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
相关资源
最近更新 更多