Question

Is it possible to dim the current view in cocoa?On a certain action I want to make the screen a little darker and prevent the user from interacting with the screen.

Was it helpful?

Solution

From the idea given by @Lord Zsolt, the same can be implemented in OS X as:

Add the following code when you want to darken your window.

NSView *transparentBlackView = [[NSView alloc] initWithFrame:[[yourwindow contentView] frame]];

CALayer *viewLayer = [CALayer layer];
[viewLayer setBackgroundColor:CGColorCreateGenericRGB(0.0, 0.0, 0.0, 0.4)]; //RGB plus Alpha Channel
[transparentBlackView setWantsLayer:YES]; 
[transparentBlackView setLayer:viewLayer];

[[yourwindow contentView] addSubview:transparentBlackView];

Also don't forget to add QuartzCore.framework to your project. And import it in your class using #import<QuartzCore/QuartzCore.h>

OTHER TIPS

Edit: I've only now noticed, it's OSX, not iOS. Though the idea should still be the same.

Disable user interaction:

[self.view setUserIntractionEnabled:NO];

Then add a UIView with black color, alpha like 0.2 above it.

UIView *transparentBlackView = [[UIView alloc] initWithFrame:self.view.frame]
[transparentBlackView setBackgroundColor:[UIColor blackColor];
[transparentBlackView setAlpha:0.2];
[self.view addSubview:transparentBlackView];

Then you can write a custom method, and use performSelector:afterDelay to remove transparentBlackView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top