Click through NSView

后端 未结 5 967
后悔当初
后悔当初 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:00

    I circumvented the issue with this code snippet.

    - (NSView *)findNextSiblingBelowEventLocation:(NSEvent *)theEvent {
      // Translate the event location to view coordinates
      NSPoint location = [theEvent locationInWindow];
      NSPoint convertedLocation = [self convertPointFromBase:location];
    
      // Find next view below self
      NSArray *siblings = [[self superview] subviews];
      NSView *viewBelow = nil;
      for (NSView *view in siblings) {
        if (view != self) {
          NSView *hitView = [view hitTest:convertedLocation];
          if (hitView != nil) {
            viewBelow = hitView;
          }
        }
      }
      return viewBelow;
    }
    
    - (void)mouseDown:(NSEvent *)theEvent {
      NSView *viewBelow = [self findNextSiblingBelowEventLocation:theEvent];
      if (viewBelow) {
        [[self window] makeFirstResponder:viewBelow];
      }
      [super mouseDown:theEvent];
    }
    

提交回复
热议问题