NSTextView NSTableView的块里面滚动

问题描述 投票:1回答:2

我有一个NSTableView每个表行内有一个NSTextView。我有我的故事板NSTextView禁用滚动。我可以滚动我NSTableView就好了,但是当我的鼠标光标在NSTextView的顶部,滚动停止。我想这是因为NSTextView被拦截滚动行为。

箭头指向NSTextView

Arrow points to the NSTextView

请记住,NSTextView是在子类NSTableViewCell

class AnnouncementsVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
  @IBOutlet weak var tableView: NSTableView!

  override func viewDidLoad() {
    //...
  }
  func numberOfRows(in tableView: NSTableView) -> Int {
    //...
  }
  func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
    //...
  }
}

class AnnouncementTableCell: NSTableCellView{
  @IBOutlet weak var message: NSTextView!
}

我如何让NSTextView通过其滚动的事件多达其父NSTableView?我的猜测是,我需要scrollWheel(with event: NSEvent)但我不确定,哪里就有奇迹,因为我在戏有两个不同的类在这里。

nstableview nstextview nsscrollview
2个回答
1
投票

实现这一目标的一种方法是子类NSScrollView和实施scrollWheel:有条件(1)无论是消费滚动的事件在你的子类的实例可以根据需要,或(2)将它们转发到一个封闭的滚动型,根据您的情况下,当前的scrollPosition。

下面是我在过去使用的相当基本的实现(的OBJ-C):

- (void)scrollWheel:(NSEvent *)e {


    if(self.enclosingScrollView) {

        NSSize
        contSize = self.contentSize,
        docSize = [self.documentView bounds].size,
        scrollSize = NSMakeSize(MAX(docSize.width  - contSize.width, 0.0f),
                                MAX(docSize.height - contSize.height,0.0f) );

        NSPoint
        scrollPos = self.documentVisibleRect.origin,
        normPos = NSMakePoint(scrollSize.width  ? scrollPos.x/scrollSize.width  : 0.0,
                              scrollSize.height ? scrollPos.y/scrollSize.height : 0.0 );

        if( ((NSView*)self.documentView).isFlipped ) normPos.y = 1.0 - normPos.y;

        if(   ( e.scrollingDeltaX>0.0f && normPos.x>0.0f) 
           || ( e.scrollingDeltaX<0.0f && normPos.x<1.0f)
           || ( e.scrollingDeltaY<0.0f && normPos.y>0.0f)
           || ( e.scrollingDeltaY>0.0f && normPos.y<1.0f) ) {

            [super scrollWheel:e];

        }
        else

            [self.nextResponder scrollWheel:e];

    }
    else 
        // Don't bother when not nested inside another NSScrollView
        [super scrollWheel:e];
}

它仍有待改进,比如独立处理DELTAX和DELTAX成分多,但也许这是足以让你的情况。


1
投票

这里有一个斯威夫特(4.2)版本的伟大工程:

class MyScrollClass: NSScrollView{
  override func scrollWheel(with event: NSEvent) {
    self.nextResponder?.scrollWheel(with: event)
  }
}

只需添加该子类的NSScrollView,它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.