事件在透明 NSWindow 中的 NSView 中不起作用

发布于 2024-10-17 09:11:55 字数 1586 浏览 2 评论 0原文

我一直

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];
    }
}

NSView 中使用这段代码,该代码被发送到后台界面生成器中,在窗口内< /strong> 我通过代码实现了部分透明但是,它**似乎只在不透明窗口内起作用,而不是在透明窗口内起作用。

为什么?我该如何解决这个问题?如果我做不到这一点,无论如何我都可以,比如拍摄整个屏幕的快照,然后将其设置(稍微变暗)作为窗口的背景?

我尝试子类化 NSWindow** 并将其放入,但它仍然不起作用。

我使用的代码实际上并没有使窗口透明,只是将其背景颜色更改为透明:

    //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]; 

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]; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的绿洲 2024-10-24 09:11:55

确保您的窗口实际上是关键窗口。也就是说,不要假设 makeKeyAndOrderFront 成功使您的窗口成为关键窗口。有关详细信息,请参阅 -[NSWindow canBecomeKeyWindow] 方法(您可能必须重写它才能使自定义窗口返回 YES)。我过去在没有标题栏的窗口中遇到过这个问题,不确定您是否遇到相同(或类似)的问题。

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.

镜花水月 2024-10-24 09:11:55

不要将 alpha 设置为 0,而是将其设置为某个较低的值,例如 0.01。对于完全透明的视图,事件将被忽略。

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

你的笑 2024-10-24 09:11:55

你在使用 UIGestureRecognizer 吗?这似乎是目前使用的最简单的方法:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文