更改窗口背景的 Alpha,而不是整个窗口

发布于 2024-10-16 11:42:56 字数 334 浏览 4 评论 0原文

所以我有一个简单的问题,我有下面的方法,它根据滑块的值设置窗口的 alpha 值,但是窗口的内容也会变得半透明,并最终随窗口一起消失。

有没有办法只更改窗口的 alpha 值而不更改其中的内容视图?

- (IBAction)changeTransparency:(id)sender { 
// Set the window's alpha value. This will cause the views in the window to redraw.
[self.window setAlphaValue:[sender floatValue]];}

谢谢,萨米。

so i have a quick question i have the method below which sets the alpha value of a window depending on the value from a slider, however the content of the window also becomes translucent and eventually disappears with the window.

Is there a way to just change the alpha value of the window and not the content view inside it?

- (IBAction)changeTransparency:(id)sender { 
// Set the window's alpha value. This will cause the views in the window to redraw.
[self.window setAlphaValue:[sender floatValue]];}

Thanks, Sami.

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

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

发布评论

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

评论(2

2024-10-23 11:42:56

Apple 的文档给出了一种方法来做到这一点。关键是将窗口的 backgroundColor 的 alpha 设置为所需的值。您还必须确保将窗口的 opaque 属性设置为 NO(默认情况下为 YES

// At some point in your code...
[window setOpaque:NO];

// Then in your changeTransparency: method...
NSColor *backgroundColor = [window backgroundColor];
backgroundColor = [backgroundColor colorWithAlphaComponent:[sender floatValue]];

[window setBackgroundColor:backgroundColor];

Apple's docs gives a way to do this. The key is to set the window's backgroundColor's alpha to the desired value. You must also make sure to set the window's opaque property to NO (which is YES by default.)

e.x.

// At some point in your code...
[window setOpaque:NO];

// Then in your changeTransparency: method...
NSColor *backgroundColor = [window backgroundColor];
backgroundColor = [backgroundColor colorWithAlphaComponent:[sender floatValue]];

[window setBackgroundColor:backgroundColor];
雪落纷纷 2024-10-23 11:42:56

这是另一种方法。

认为,
self.window <--- 基本视图并且这个 alpha 将被更改(但完全是假的)。
subView1, subView2 <-- 这些视图是 self.window 的内容。并且不应更改其 alpha 值。

self.window.backgroundColor = [UIColor clearColor];

UIView* anAlphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.widht, self.window.frame.size.height)];
anAlphaView.backgroundColor = [UIColor blackColor]; // as you want
anAlphaView.alpha = 0.5f; // as you want.
[self.window addSubview:anAlphaView];
[anAlphaView release];

[self.window addSubview:subView1]; // you should add sub views to self.window
[self.window addSubview:subView2];

您可以使用上面的代码创建一个方法:)

Here is another way.

Suppose,
self.window <--- base view and this alpha will be changed (but exacatly fake).
subView1, subView2 <-- these views are contents of self.window. and theier alpha should not be changed.

self.window.backgroundColor = [UIColor clearColor];

UIView* anAlphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.widht, self.window.frame.size.height)];
anAlphaView.backgroundColor = [UIColor blackColor]; // as you want
anAlphaView.alpha = 0.5f; // as you want.
[self.window addSubview:anAlphaView];
[anAlphaView release];

[self.window addSubview:subView1]; // you should add sub views to self.window
[self.window addSubview:subView2];

You can make a method using above code :)

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