Dim the view as disabled

How do you obscure the view as if it were disabled / highlighted, preferably without any additional views?

By sight, I mean a UIView with all its children. I want to achieve the same effect of a disabled / highlighted UIButton .

Do not assume that the view is completely opaque.

+3
source share
3 answers

What I'm playing with:

  • Create a black layer with opacity ( _highlightLayer ). This is similar to the black view with alpha approach.
  • _highlightLayer mask with an opaque image of the original view.
  • Add _highlightLayer to the presentation layer.

Only opaque view pixels will be darkened.

Code:

 - (void)highlight { // Black layer with opacity _highlightLayer = [CALayer layer]; _highlightLayer.frame = CGRectMake(0, 0, self.layer.bounds.size.width, self.layer.bounds.size.height); _highlightLayer.backgroundColor = [UIColor blackColor].CGColor; _highlightLayer.opacity = 0.5; // Create an image from the view UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0); [self.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *maskImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Create a mask layer for the black layer CALayer *maskLayer = [CALayer layer]; maskLayer.contents = (__bridge id) maskImage.CGImage; maskLayer.frame = _highlightLayer.frame; _highlightLayer.mask = maskLayer; [self.layer addSublayer:_highlightLayer]; } 

And then:

 - (void)unhighlight { [_highlightLayer removeFromSuperlayer]; _highlightLayer = nil; } 

Of course, this should only be used for small views.

+8
source

Example:

You have a button UIButton * button1 and UIView * view1

You can disable the button and view this way:

 [view1 setHidden:YES]; [button1 setEnabled:NO]; 

Button inclusion and presentation can be done as follows:

 [view1 setHidden:NO]; [button1 setEnabled:YES]; 

Hope this helps.

0
source

While this approach doesn't suit your preference for not using extra views, just adding a black view with alpha 0.6 or so seems to have an effect, with the added benefit that you can use this new view to intercept UIEvents so that everyone subzones are effectively disabled in droves.

You can even draw graphically and instead of just using a black background for an overlay view, you can fill its background with a radial gradient to achieve the effect that happens in iOS when a pop-up view disables the view behind it ...

0
source

Source: https://habr.com/ru/post/957653/


All Articles