NSSplitView:自定义双击处理程序



我正在尝试找出如何检测双击NSSplitView分隔器。在我看来,分隔线既不向 NSSplitViewDelegate 公开,也不向 NSSplitViewController 公开。

到目前为止,我发现分隔符是NSSplitDividerView的一个实例,它是一个私有类,因此不能扩展或子类化它。

你能让我回到正确的调查轨道上吗?

谢谢。

一个可能的解决方案是子类NSSplitView并覆盖mouseDown(with:) 。检查事件的位置是否在子视图之间。

override func mouseDown(with event: NSEvent) {
    if event.clickCount == 2 {
        let location = self.convert(event.locationInWindow, from: nil)
        if location.x > NSMaxX(self.arrangedSubviews[0].frame) && location.x < NSMinX(self.arrangedSubviews[1].frame) {
            Swift.print("doubleclick")
            return
        }
    }
    super.mouseDown(with: event)
}

我很确定您可以在拆分视图的委托对象中实现 optional func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool 方法,返回 false ,然后实现您想要的任何自定义操作。