简体   繁体   中英

Events not working in NSView in transparent NSWindow

I've been using this code...

static const CGFloat kSwipeGestureLeft  =  1.0;
static const CGFloat kSwipeGestureRight = -1.0;
static const CGFloat kSwipeGestureUp    =  1.0;
static const CGFloat kSwipeGestureDown  = -1.0;

- (void)swipeWithEvent:(NSEvent *)event {

    if ([event deltaX] == kSwipeGestureLeft) {

        NSLog(@"LEFT SWIPE!");
    } 
    else if ([event deltaX] == kSwipeGestureRight) {

        NSLog(@"RIGHT SWIPE!");
    } 
    else if ([event deltaY] == kSwipeGestureUp) {

        NSLog(@"UP SWIPE!");
    } 
    else if ([event deltaY] == kSwipeGestureDown) {

        NSLog(@"DOWN SWIPE!");
    } 
    else {
        [super swipeWithEvent:event];
    }
}

in an NSView which was sent to back in Interface Builder , inside a window that I made partially transparent through code . However , it **only seems to work when inside an opaque window, not a transparent window.

Why? How can I fix this? If I can't do this is there anyway I can, like taking a snap of the entire screen then setting that (slightly darkened) as the background of the window?

I tried subclassing NSWindow** and putting this in, but it still didn't work.

The code I'm using isn't actually making the window transparent, just change it's background colour to transparent:

    //Set up the window

[window setLevel:kCGNormalWindowLevel];
[window setOpaque:NO];
[window setStyleMask:0];
[window setBackgroundColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.3]];
[window setAlphaValue:0];


    //Resize the window to fill the screen

[window
 setFrame:[window frameRectForContentRect:[[window screen] frame]]
 display:YES
 animate:YES];

    //Fade the window in

[window makeKeyAndOrderFront:self];
[[window animator] setAlphaValue:1.0]; 

Make sure your window is in fact the key window. That is, don't assume makeKeyAndOrderFront succeeded in making your window the key window. See the -[NSWindow canBecomeKeyWindow] method for details (you may have to override it for your custom window to return YES). I've run into this problem in the past for windows that don't have a title bar, not sure if you are encountering the same (or a similar) problem.

Do not set alpha to 0, set it to some low value like 0.01 instead. Events are ignored for completely transparent views.

Are you using UIGestureRecognizer? It seems to be the simplest method to use as of now :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM