滚动时NSScrollView中的NSTextView不配合 [英] NSTextView inside NSScrollView not co-operating while scrolling

查看:340
本文介绍了滚动时NSScrollView中的NSTextView不配合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableView,其中有NSTextView作为单元格。
这是我在scrollView里面有scrollView的情况。现在,当我滚动textView的内容时,以及当textview的内容到达末尾时,我都希望父级scrollView(tableView)继续滚动。但是默认情况下不会发生。相反,当鼠标指针位于textView内时,父级根本不会滚动。

I have a TableView which has NSTextView as cells. this is a situation where I have scrollView inside scrollView. Now when I scroll the contents of the textView, and when the textview's content reaches the end, I want the parent scrollView ( the tableView ) to continue scrolling. But by default this does not happen. Instead the parent doesn't scroll at all when the mouse pointer is inside the textView.

我想在此示例中实现类似的目的。

这是我的解决方案:

public class DisableableScrollView: NSScrollView {

public override func scrollWheel(with event: NSEvent) {

    // Check if the text field is empty
    if (self.subviews[0].subviews[2] as! NSTextView).textStorage?.length == 0 {
        nextResponder?.scrollWheel(with: event)
    }
    // Bottom
    else if self.verticalScroller?.floatValue == 1.00  {
        if event.deltaY < 0 {
            nextResponder?.scrollWheel(with: event)
        }
        else {
            super.scrollWheel(with: event)
        }
    }
    // Top
    else if self.verticalScroller?.floatValue == 0.00  {
        if event.deltaY > 0 {
            nextResponder?.scrollWheel(with: event)
        }
        else {
            super.scrollWheel(with: event)
        }
    }

    else {
        super.scrollWheel(with: event)
    }
}

但是有一个问题:当我在一个文本字段上滚动时,另一个文本字段也在滚动。单击此处查看

But there is a problem: when I scroll on one text field other text field is scrolling.click here to see

这是魔术鼠标的问题。如果我使用普通的强大鼠标,则一切正常。但是使用魔术鼠标时,在滚动后抬起手指时,它将继续滚动(动量滚动),但有时会滚动错误的textView实例。根据 Apple文档,滚轮事件有两个属性:相位和动量相位。在我抬起手指之后,使用魔术鼠标会变成Ended(结束),并且动量阶段会开始。

Here the problem is with Magic mouse. If I use a normal mighty mouse, everything works fine. But with magic mouse, when I lift my finger after scrolling, it continues to scroll ( momentum scroll ) but scrolls wrong textView instance sometimes. As per Apple Documentation, there are two properties for a scrollwheel event : phase and momentumPhase. With magic mouse after I lift my finger up phase becomes Ended and momentumPhase becomes Began.

有人知道这个问题的标准解决方案吗?
还是我的代码正确无误?

Does anyone know a standard solution to this problem? Or if my code is correct what might be going wrong?

推荐答案

我找到了解决问题的方法,因此想到共享它。

I found the solution to my problem and hence thought of sharing it.

这种特殊方法 wantsForwardedScrollEvents(for :) 可用于在内容到达末尾时沿响应者链向上转发滚动事件。我们只需要重写此方法,并在必要时返回 true

This particular method wantsForwardedScrollEvents(for:) can be used for forwarding the scroll events up the responder chain whenever the content has reached the end. We just have to override this method and return true wherever necessary.

这篇关于滚动时NSScrollView中的NSTextView不配合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆