简体   繁体   English

垂直滚动 NSTableView Swift macOS 中的水平 NSCollectionView

[英]Horizontal NSCollectionView in vertically scrolling NSTableView Swift macOS

I created a vertically scrolling NSTableView.我创建了一个垂直滚动的 NSTableView。 Each cell of this table view contains a title and beneath is a horizontally scrolling NSCollectionView.这个表视图的每个单元格都包含一个标题,下面是一个水平滚动的 NSCollectionView。 When the cursor is on the title when scrolling, the vertical tableview scrolls fine, but when the cursor is on the horizontally scrolling collection view the table view doesn't scroll.当滚动时光标位于标题上时,垂直 tableview 滚动得很好,但是当光标位于水平滚动的 collection view 上时,table view 不会滚动。

How can I get the table view to scroll even when the cursor is on the collection view?即使光标位于集合视图上,如何让表视图滚动? I understand that the tableview and scrollview use two separate scroll views, but I don't know if there is a way to synchronize these two.我知道tableview和scrollview使用两个独立的滚动视图,但是不知道有没有办法同步这两个。

Ok thank god, I found a solution here on stackoverflow after all.好的,感谢上帝,毕竟我在 stackoverflow 上找到了解决方案。 It was in Objective-C so I translated it to Swift.它是在 Objective-C 中的,所以我将它翻译成 Swift。 Here it is for anybody looking at a similar problem:这是针对任何正在寻找类似问题的人:

class MTHorizontalScrollView: NSScrollView {

var currentScrollIsHorizontal = false

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)
}

override func scrollWheel(with event: NSEvent) {
    if event.phase == NSEvent.Phase.began || (event.phase == NSEvent.Phase.ended && event.momentumPhase == NSEvent.Phase.ended) {
        currentScrollIsHorizontal = fabs(event.scrollingDeltaX) > fabs(event.scrollingDeltaY)
    }
    if currentScrollIsHorizontal {
        super.scrollWheel(with: event)
    } else {
        self.nextResponder?.scrollWheel(with: event)
    }
}

}

Full credit to SO LINK完全归功于SO LINK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM