2011-02-08 68 views
0

所以我有一个快速问题我有下面的方法根据滑块的值设置窗口的alpha值,但窗口的内容也变得半透明,最终随着窗户消失。更改窗口背景的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]];} 

谢谢,萨米人。

回答

2

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

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

这是另一种方法。

假设, self.window < ---基本视图和这个alpha将被改变(但是非常虚假)。 subView1,subView2 < - 这些视图是self.window的内容。而不应该改变它。

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

您可以使用上面的代码做的方法:)