How to make a transparent NSView subclass handle mouse events?

后端 未结 7 1595
清歌不尽
清歌不尽 2021-02-04 10:31

The problem

I have a transparent NSView on a transparent NSWindow. The view\'s drawRect: method draws some content (NSIma

7条回答
  •  醉酒成梦
    2021-02-04 10:48

    You can override the hitTest method in your NSView so that it always returns itself.

    According to the NSView documentation, the hitTest method will either return the NSView that the user has clicked on, or nil if the point is not inside the NSView. In this case, I would invoke [super hitTest:], and then return the current view only if the result would otherwise be nil (just in case your custom view contains subviews).

    - (NSView *)hitTest:(NSPoint)aPoint
    {
        NSView * clickedView = [super hitTest:aPoint];
        if (clickedView == nil)
        {
            clickedView = self;
        }
    
        return clickedView;
    }
    

提交回复
热议问题