水平和垂直嵌套滚动视图

时间:2016-03-15 11:51:02

标签: swift macos user-interface cocoa nstableview

我的视图包含一个水平滚动的滚动视图,其中包含多个垂直滚动的表视图。

见图:

enter image description here

但是,当光标位于Scroll视图中时,表视图会阻止水平滚动。

如何让滚动传递到超级视图?

申请代码

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

     @IBOutlet weak var window: NSWindow!
    var viewController: ViewController!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        viewController = ViewController(nibName: "ViewController", bundle: nil)  
        viewController.view.translatesAutoresizingMaskIntoConstraints = false
        window.contentView!.addSubview(viewController.view)
        //...
    }
    //...
}

查看控制器

class ViewController: NSViewController {

    @IBOutlet weak var scrollView: NSScrollView!
    //...

    func addListView(listPath: String) {
        let listView = ListViewController(nibName: "ListView", bundle: nil)
        listView!.listPath = listPath

        listView!.view.translatesAutoresizingMaskIntoConstraints = false
        scrollView.contentView.addSubview(listView!.view)
        //...
    }
    //...
}

列表视图

class ListViewController: NSViewController {

    @IBOutlet weak var itemsTitleView: NSTextField!
    @IBOutlet weak var itemsTableView: MyTableView!
    //...
}

的TableView

class MyTableView: NSTableView {

    override func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)

        // Drawing code here.
    }

    var mainScrollView: NSScrollView? {
        let app = NSApplication.sharedApplication()
        let delegate = app.delegate as! AppDelegate
        return delegate.viewController.scrollView
}

    override func mouseDown(theEvent: NSEvent) {
        // do nothing
    }

    override func scrollWheel(theEvent: NSEvent) {
        if theEvent.scrollingDeltaY == 0 {
            // It's a horizontal scroll -> redirect it
            if let tableScrollView = enclosingScrollView, mainScrollView = mainScrollView {
                mainScrollView.scrollWheel(theEvent)
            }
        } else {
            // It's a vertical scroll -> behave normally
            super.scrollWheel(theEvent)
        }
    }

}

2 个答案:

答案 0 :(得分:0)

我要尝试的第一件事是抓住滚动事件,因为它被传递到表格视图。确定事件对象表示水平滚动手势后,将其重定向到主滚动视图。

class MyTableView: NSTableView {

    var mainScrollView: NSScrollView? {
        // Your tables will need some way to access the main scroll view.
        // I'm assuming here that there's a reference to it on the AppDelegate
        let app = NSApplication.sharedApplication()
        let delegate = app.delegate as! AppDelegate
        return delegate.mainScrollView
    }


    override func scrollWheel(theEvent: NSEvent) {

        if theEvent.scrollingDeltaY == 0 {
            // It's a horizontal scroll -> redirect it
            if let tableScrollView = enclosingScrollView, mainScrollView = self.mainScrollView {
                mainScrollView.scrollWheel(theEvent)
            }
        } else {
            // It's a vertical scroll -> behave normally
            super.scrollWheel(theEvent)
        }
    }
}

我不得不说,无论何时你开始搞乱这样的事件,你都需要为副作用做好准备;在我为测试此代码而创建的简单演示应用程序中,它似乎工作正常,但如果这种方法引发其他挑战,请不要感到惊讶。

答案 1 :(得分:0)

正如Paul所指出的那样,使用他的方法可能会有一些细微的问题-使用与他的答案非常相似的方法时,我已经看到了NSTrackingArea的问题。

有关更可靠的方法,请参见雅各布对类似问题的回答:

NSScrollView inside another NSScrollView

相关问题