如何使一个透明的NSView子类处理鼠标事件? [英] How to make a transparent NSView subclass handle mouse events?

查看:1514
本文介绍了如何使一个透明的NSView子类处理鼠标事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我在透明的 NSWindow上有一个透明的 NSView code>。视图的 drawRect:方法绘制一些内容( NSImage s, NSBezierPath s和 NSString s)在视图上,但它的部分是透明的。



已绘制的视图调用常用的鼠标事件处理方法( mouseDown: mouseUp:)。 p>

点击透明区域,可以聚焦到透明窗口后面的任何窗口。



使透明区域的部分可点击,以便在我的视图上绘制的元素之间意外点击不会导致窗口失去焦点。






已尝试的解决方案




  • 覆盖 NSView s hitTest:方法。发现 hitTest:仅在点击视图的不透明区域时调用。

  • 覆盖 NSView opaqueAncestor 方法。

  • 使用 [NSColor clearColor] 填充透明区域的某些部分。在 drawRect:方法中,并使用几乎但不完全透明的颜色。

  • 尝试了 NSTrackingArea 类。这似乎只添加对 mouseEntered: mouseExited: mouseMoved: cursorUpdate:方法, mouseUp: $ c> mouseDown:


解决方案

知道,单击事件透明部分的窗口不会传递到你的应用程序,所以没有一个正常的事件链覆盖(即-hitTest:,-sendEvent:等)将工作。我唯一能想到的头顶的方法是使用Quartz Event Taps来捕获所有的鼠标点击,然后手动判断他们是否在窗口的透明区域。坦白说,这听起来像一个巨大的PITA,没有多少收获。


The problem

I have a transparent NSView on a transparent NSWindow. The view's drawRect: method draws some content (NSImages, NSBezierPaths and NSStrings) on the view but leaves parts of it transparent.

Clicking on the regions of the view that have been drawn on invokes the usual mouse event handling methods (mouseDown: and mouseUp:).

Clicking on the transparent areas gives focus to whatever window is behind my transparent window.

I would like to make parts of the transparent region clickable so that accidentally clicking between the elements drawn on my view does not cause the window to lose focus.


Solutions already attempted

  • Overriding the NSView's hitTest: method. Found that hitTest: was only called when clicking on a non-transparent area of the view.
  • Overriding the NSView's opaqueAncestor method. Found that this was not called when clicking on any part of the view.
  • Filling portions of the transparent area with [NSColor clearColor] in the drawRect: method, and with an almost-but-not-quite-transparent colour. This had no effect.
  • Experimented with the NSTrackingArea class. This appears to only add support for mouseEntered:, mouseExited:, mouseMoved:, and cursorUpdate: methods, not mouseUp: and mouseDown:.

解决方案

As far as I know, click events to transparent portions of windows aren't delivered to your application at all, so none of the normal event-chain overrides (i.e -hitTest:, -sendEvent:, etc) will work. The only way I can think of off the top of my head is to use Quartz Event Taps to capture all mouse clicks and then figure out if they're over a transparent area of your window manually. That, frankly, sounds like a huge PITA for not much gain.

这篇关于如何使一个透明的NSView子类处理鼠标事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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