事件在透明 NSWindow 中的 NSView 中不起作用
我一直
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的窗口实际上是关键窗口。也就是说,不要假设 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.
不要将 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.
你在使用 UIGestureRecognizer 吗?这似乎是目前使用的最简单的方法:)
Are you using UIGestureRecognizer? It seems to be the simplest method to use as of now :)