2016-03-15 132 views
0

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

参见图:

enter image description here

然而,表视图是在滚动视图阻断水平滚动时光标在它们。

我该如何让滚动传递给超级视图?


代码应用

@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) 
     } 
    } 

} 
+0

表视图是否需要水平滚动? –

+0

嗨保罗,没有表视图将永远不会水平滚动。 – fff

+0

哎呀,我的坏,有内部表视图周围的自定义视图 – fff

回答

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) 
     } 
    } 
} 

但是我不得不说,任何时候当你开始搞这样的事情时,你需要为副作用做好准备;在我为测试此代码而创建的简单演示应用程序中,它似乎可以正常工作,但如果此方法带来更多挑战,请不要感到惊讶。

+0

非常感谢。只有在这里触控板才会沿着这些方向读取解决方案,并让您知道我如何继续。 – fff

+0

现在假设scrollWheel是一个通用函数,在我的情况下,scrollWheel函数不会调用,例如添加到它的任何'print'语句。 – fff

+0

是的,''scrollWheel:''是一个通用功能,所以它可以与触控板配合使用。我的猜测是它没有被调用,因为你在某个地方犯了一个简单的错误。你已经创建了你的子类的表实例吗? “scrollWheel:”的签名与我的代码完全相同吗?如果你重写''mouseDown:'',那么它会被忽略吗? –