NSNotification 滚动 - 用户与编程
我有一个带有 NSTextView (实际上是自定义子类)的程序,可能会以编程方式插入很多行数据。 (它从 USB 端口读取串行数据流。)我有一个用于启用/禁用自动滚动的复选框。我想让用户只需尝试向上滚动即可摆脱自动滚动。因此,我需要一个通知来告诉我用户何时滚动,而不仅仅是边界何时更改,因为每次插入更多串行数据时都会发生这种情况。这可能吗?
I have a program with an NSTextView (actually, a custom subclass) into which a lot of lines of data are likely to be programmatically inserted. (It reads a stream of serial data from a USB port.) I have a checkbox for enabling/disabling autoscrolling. I want to allow the user to break out of autoscrolling simply by trying to scroll back up. So, I need a notification that tells me when the user has scrolled, not just when the bounds have changed, since this happens every time more serial data gets inserted. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,您可以使用通知来告诉您何时发生任何滚动,然后检查文本视图是否完全滚动到底部?如果是,请打开自动滚动。如果没有,请将其关闭。
Surely you could use the notification that tells you when any scrolling takes place, and then check if the text view is scrolled entirely to the bottom? If it is, turn auto-scrolling on. If not, turn it off.
这不是最好的,但这适用于我的情况。我认为通过一些 NSEvent 操作可以做得更干净一些,但我意识到我可以通过检查当前滚动位置与总文档矩形高度来检查用户是否已开始滚动。
因此,基本上,如果滚动位置变为程序期望的编程写入以外的位置,则会关闭自动滚动。此实现的一个很酷的事情是,如果您添加
else [autoscrolls setState:1];
,当您向下滚动以赶上流时,它会重新打开自动滚动。当您运行具有大量输出的 shell 脚本时,这会模拟终端中的滚动行为,例如 Fedora 上的yum install
或类似的事情。It's not the best, but this works in my case. I think it could be done a little cleaner with some NSEvent manipulation, but I realized that I can check to see if the user has started scrolling by checking the current scroll position against the total document rectangle height.
So basically, if the scroll position becomes anything other than what the program expects it to be from programmatic writes, it turns autoscrolling off. A cool thing about this implementation is that if you ad an
else [autoscrolls setState:1];
, it turns autoscrolling back on when you scroll back down to catch up with the stream. This emulates the scrolling behavior in the terminal when you're running a shell script with lots of output, like ayum install
on Fedora or that sort of thing.