0

I want to create in my App something like that:

enter image description here

I can open the camera and add the background 50% grey but the problem I have now, is how I can create a transparent square over the background and put in the middle a red line.

[self.view bringSubviewToFront:self.background];

If I add the next line appear the square but appear the background.

[self.view bringSubviewToFront:self.transparentSquare];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
reizintolo
  • 429
  • 2
  • 7
  • 20

1 Answers1

0

The problem is that self.background is too simple. It is solid gray, so it appears as solid gray even behind your transparent square. You need to "punch a hole" in self.background so that it is transparent at the place where the transparent square will go. You can easily do that with a mask.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • See my answer here http://stackoverflow.com/a/23452614/341994 for more detail about how to punch a rectangular hole in a view using a mask. – matt Jan 11 '15 at 18:56
  • Works perfect!, about the: CGRect r2 = CGRectMake(20,20,40,40);for use with several devices like iphone 5 or 6... is proportional or I have to implement a control to calculate de size of the screen? – reizintolo Jan 11 '15 at 22:09
  • What I would do is: use auto layout to position the transparent square. After layout, you know where the transparent square is. Now use that info to figure out where to position the hole in the mask. This will need to change every time the transparent square changes its position, obviously. – matt Jan 11 '15 at 22:13
  • I follow your idea, and I put in the center the transparent square and assign in the code...but I was testing with several triggers like: viewWillAppear, viewDidLoad, to get the position of the transparent square with CGRect r2 = self.transparentLayout.bounds;, but the problem is that position is not set up yet, what is the trigger I can use, to get the final position with AutoLayout? – reizintolo Jan 12 '15 at 07:59