【问题标题】:Control-click does not change selection in IKImageBrowserView, right-click or two-finger-click doesControl-click 不会更改 IKImageBrowserView 中的选择,右键单击或两指单击会更改
【发布时间】:2015-11-23 20:43:05
【问题描述】:

我向IKImageBrowserView 添加了一个上下文菜单。

当用户右键单击(鼠标)或两指单击(触控板)IKImageBrowserView 中的图像时,选择更改为该图像,并出现上下文菜单。

当用户控制点击(鼠标或触控板)时,选择不会改变,并且会出现上下文菜单。

由于上下文菜单与所选图像相关,因此我更喜欢在调用上下文菜单时选择更改。

  • 我可以在控制单击(鼠标和触控板)时也进行IKImageBrowserView 更改选择吗?
  • 我可以不将上下文菜单附加到 IKImageBrowserView 而是附加到 IKImageBrowserView 的单个元素/图像吗?

【问题讨论】:

    标签: macos cocoa mouse ikimagebrowserview trackpad


    【解决方案1】:

    如果你创建一个 IKImageBrowserView 的子类并覆盖menuForEvent:,你可以做到这一点:

    - (NSMenu *)menuForEvent:(NSEvent *)event {
        NSUInteger idx = [self indexOfItemAtPoint:[self convertPoint:[event locationInWindow] fromView:nil]];
    
        if (idx == NSNotFound) {return nil;}
    
        if (![self.selectionIndexes containsIndex:idx]) {
            [self setSelectionIndexes:[NSIndexSet indexSetWithIndex:idx] byExtendingSelection:NO];
    
        }
    
        return self.menu;
    }
    

    【讨论】:

    • 这在原理上是有效的,但是当我点击 IKImageBrowserView 内但在实际图像之外的区域时会出错:-[NSIndexSet initWithIndexesInRange:]: Range {9223372036854775807, 1} exceeds maximum index value of NSNotFound - 1
    • 好的,9223372036854775807 似乎是NSNotFound 的值,我可以在更改索引之前测试(idx != NSNotFound)。我更新了你的答案。
    • 啊...我已经将它从无法在行之间单击的 tableView 中使用进行了调整,并且没有在 IKImageBrowserView 上尝试。
    猜你喜欢
    • 2013-03-23
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多