Click through NSView

后端 未结 5 966
后悔当初
后悔当初 2021-01-02 04:30

I have an NSView containing multiple subviews. One of those subviews is transparent and layered on top.

I need to be able to click through this view dow

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 05:03

    Here's a Swift 5 version of Erik Aigner's answer:

    public override func mouseDown(with event: NSEvent) {
        // Translate the event location to view coordinates
        let convertedLocation = self.convertFromBacking(event.locationInWindow)
    
        if let viewBelow = self
            .superview?
            .subviews // Find next view below self
            .lazy
            .compactMap({ $0.hitTest(convertedLocation) })
            .first
        {
            self.window?.makeFirstResponder(viewBelow)
        }
    
        super.mouseDown(with: event)
    }
    

提交回复
热议问题