I currently have two UIViews: one of a red background and the other blue. The blue view is a subview of the red view. What I would like to do is be able to "cut" out rectangles on the blue view so that the red view can be visible. How do you go about doing this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
to draw an ellipse, instead of a rect, just set the blendMode to clear:
These may be the dumb ways but I would not do it the way you describe, but make it look the way you want it to look.
(Jacques' answer just popped up - looks good to me)
Approach 1: In the view controller to build a list of rectangles that tile around the exposed "hole". As your holes increase in number the number of tiling rects will also increase.
Approach 2: Reverse your thinking. The blue view should be at the back with sections of the red view placed on top of it. You still see a red view with all but the "holes" masked by the blue view, but what you are really doing is copying regions from the view you want to expose and putting them on top of the mask as you make each hole. If you have some effect simulating depth you could add that as required with each hole.
Neither requires subclassing or drawRect:.
You have to override the top view's
drawRect
method. So, for example, you might create aHoleyView
class that derives fromUIView
(you can do that by adding a new file to your project, selecting Objective-C subclass, and setting "Subclass of" toUIView
). InHoleyView
,drawRect
would look something like this:If you're using Interface builder, make sure to change the holey view's class to
HoleyView
. You can do that by selecting in the view in Interface Builder and selecting the "Identity" pane in the inspector (its the one on the far right the the "i" icon).You also have to set the top view to be non-opaque either with the following code snippet, or by un-checking the
Opaque
checkbox in the view's properties in Interface Builder (you'll find it in the View section of the view's attributes) and set its background color's opacity to 0% (background color is set in the same section).If you want to do circles, you have to use
Core Graphics
(aka Quartz 2D). You'll probably want to read the programming guide, which is available here.To draw an ellipse instead of the rectangle, your
drawRect
would look something like this:There is truth in all the other answers, but it is quite possible to draw with clear colour, or so to say erase the existing colours within any path, even with the -[UIBezierPath fill] or similar convenience methods. All you have to do is to set the context blend mode to an appropriate value depending on the effect you are trying to achieve, like so:
There are around two dozen different options you could choose from, take a look around the CGContext reference.