覆盖scrollWheel函数时,NSScrollView中的滚动停止 [英] Scrolling in NSScrollView stops when overwriting scrollWheel function

查看:205
本文介绍了覆盖scrollWheel函数时,NSScrollView中的滚动停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我覆盖NSScrollView中的scrollWheel(theEvent:NSEvent)函数时,遇到一个奇怪的错误.

I experience a weird bug when I overwrite the scrollWheel(theEvent: NSEvent) function in a NSScrollView.

这是我的代码:

import Cocoa

class GraphScrollView: NSScrollView {
    var axisScrollViewInstance: AxisScrollView?

    override func scrollWheel(theEvent: NSEvent) {
        if theEvent.deltaY != 0 {
            axisScrollViewInstance?.scrollEventFromOtherScrollView(theEvent)
            super.scrollWheel(theEvent)
        } else if theEvent.deltaX != 0 {
            super.scrollWheel(theEvent)
        }
    }
}

class AxisScrollView: NSScrollView {
    var graphScrollViewInstance: GraphScrollView?
    override func scrollWheel(theEvent: NSEvent) {
        if theEvent.deltaY != 0 {
            super.scrollWheel(theEvent)
            graphScrollViewInstance?.scrollWheel(theEvent)
        }
    }

    func scrollEventFromOtherScrollView(theEvent: NSEvent) {
        if theEvent.deltaY != 0 {
            super.scrollWheel(theEvent)
        }
    }   
}

代码基本上检查用户向哪个方向滚动,如果方向垂直,则将NSEvent转发到另一个NSScrollView.我需要这样做,这样我才能使两个NSScrollView彼此相邻,并且它们都垂直滚动,并且其中一个也水平滚动.例如,您可以查看Mac Calendar应用程序,该应用程序的左侧有一个小时列,只能垂直滚动,而右侧有一个星期概览,可以水平和垂直滚动.

The code basically checks in which direction the user is scrolling and forwards the NSEvent to another NSScrollView if the direction is vertically. I need this so that I can have two NSScrollViews next to each other, which both scroll vertically and one of them scrolls horizontally as well. As an example you can look at the Mac Calendar app, which has an hour column on the left that only scrolls vertically and a week overview on the right that scrolls horizontally and vertically.

但是,如果我覆盖scrollWheel方法并从内部调用super函数,则会导致Mighty Mouse出现怪异的问题.我只能向上和向右滚动,但是如果我沿任何其他方向滚动,它都不会执行任何操作,尽管始终会发生NSEvent,而无论我沿哪个方向滚动.一段时间后,它完全停止滚动.

However, if I overwrite the scrollWheel-method and call the super function from inside, it leads to a weird problem with the Mighty Mouse. I can only scroll upwards and to the right, whereas it does nothing if I scroll in any other direction, although a NSEvent is always occurring, disregarding in which direction I scroll. After a while it stops scrolling altogether.

我已经尝试过简单地覆盖scrollWheel方法,并且仅使用如下事件调用超级函数:

I have already tried to simply overwrite the scrollWheel-method and only call the super function with the event like this:

override func scrollWheel(theEvent: NSEvent) {
    super.scrollWheel(theEvent)
}

但是会导致同样的问题.我将第二个(非Apple)鼠标连接到我的系统,该鼠标具有正常"滚轮,并且我的代码可以正常运行.

but it leads to the same problems. I have a second (non-Apple) mouse connected to my system, which has a 'normal' scroll wheel and with which my code works perfectly fine.

您是否知道可能是什么问题?我听说NSEvents是不同的子类型(1:Apple Device,0:任何其他Device).也许与它有关? 否则,您知道我如何使两个NSScrollView彼此相邻并同时垂直滚动吗?

Do you have an idea what the problem could be? I heard that the NSEvents are of different subtype (1: Apple Device, 0: Any other Device). Maybe that has something to do with it? Otherwise, do you know how I can have two NSScrollViews next to each other that scroll vertically simultaneously?

推荐答案

好的,所以这是Xcode 6.4中以及可能在早期版本中的Interface Builder的一个错误.您必须确保选中了NSScrollView的属性检查器中的显示垂直滚动条"和显示水平滚动条"框.

Okay, so it's a bug with the Interface Builder in Xcode 6.4 and maybe in earlier versions as well. You have to make sure that the box "Show Vertical Scroller" and "Show Horizontal Scroller" in the Attribute Inspector of the NSScrollView are checked.

如果同时选中了两个框,则覆盖scrollWheel(theEvent:NSEvent)函数不会有问题.然后,您可以通过设置响应滚动功能.

If you check both boxes you won't have problems with overwriting the scrollWheel(theEvent: NSEvent) function. You can then remove the scroll bars programmatically again without problems by setting the hasHorizontalScroller and hasVerticalScroller to false in e.g. your init()


Update at 12.10.2015
I've reported the 'bug' to Apple, however it seems that by overwriting the -scrollWheel function, the view loses its responsive scrolling ability.

这是Apple开发团队的答案:

This is the answer of the Apple Dev Team:

  1. 如果覆盖-scrollWheel,则该scrollView无法执行响应滚动.
  2. 如果未启用响应滚动,则必须显示滚动条,scrollView才能滚动.
  3. 请参阅在文档中说:

    此方法的默认实现返回true,除非该类重写lockFocus或scrollWheel:方法

    The default implementation of this method returns true unless the class overrides the lockFocus or scrollWheel: method

    这篇关于覆盖scrollWheel函数时,NSScrollView中的滚动停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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