Search code examples
ioscore-animationavfoundation

Is it possible to add a transparent layer or view above a colored view?


I'm using AVFoundation framework to scan a barcode, but that may be unrelevant for my problem.

What I want:

I would like that the square bordered in green be transparent (not with the darkened black).

enter image description here

Here is what I have done: I have 2 views: backgroundView( which occupies the whole screen) and highlightView which is the square bordered with green, on top of backgroundView (I have used a XIB for dimensions and positions) :

self.highlightView.layer.borderColor = [UIColor greenColor].CGColor;
self.highlightView.layer.borderWidth = 3;
// this following line does not allow the square to be transparent
self.highlightView.layer.opacity = 0;

// relative to AVFoundation
_previewLayer.frame = _backgroundView.bounds;
[_backgroundView.layer addSublayer:_previewLayer];

_previewLayer.backgroundColor = [UIColor blackColor].CGColor;
_previewLayer.opacity = 0.3;

UPDATE : xib (here representing the square with a clear color background), the backgroundView has the property black color background).

enter image description here


Solution

  • As I mentioned, you were looking in the wrong direction. There are multiple posts with a problem similar to yours, which have pretty decent answers. (You will have to study and understand to make the most of them):

    To sum it up, you need to apply the semi-transparent color to the layer of backgroundView and then play around with the layer's mask property to get the work done.

    You can find many tutorials to learn using the layer and mask together.

    Hope this helps.