Anna Fortuna
Anna Fortuna

Reputation: 1071

Drawing with two CALayers on top of each other

I have 2 views and I want to use their respective layers for drawing. My first view, which I'll call the TextView, is nothing but a page full of text. The other view, which I'll call DrawView, is where the drawing happens. In TextView, the layer is used to draw the text of the PDF into the view. For example:

View showing contents of PDF

The DrawView's layer on the other hand is where the custom drawing is being made. (e.g. freeform drawings, rectangles, etc.)

Now what I want to happen is to have the drawings in the DrawView appear as if they are being drawn under the TextView. Like this:

Highlighted text

What I did was to add DrawView as a subview of TextView. But this is what happened:

text covered with color

I tried using the kCGBlendModeMultiply like what was used in the second image, but nothing happens. Can anyone tell on how to make this work? Thanks.

NOTE: The only reason that the second image achieved what I want is because I inserted the rectangle inside the same layer, which is the TextView's layer. I want to do the same effect but inside the DrawView layer, which is a subview of the TextView.

Upvotes: 1

Views: 470

Answers (1)

bkbeachlabs
bkbeachlabs

Reputation: 2171

Just an idea:

You can use 3 layers, instead of 2. The highlighted region can be on layer 1 (the back layer). The text document can be on layer 2 (middle) and the drawing layer can be on layer 3 (the front layer).

Set the opacity of layer 3 to some intermediate value, so that you can still see what's on layer 2.

When the user draws, have layer 3 be the view that actually accepts the drawing. Since it is semi-transparent, you will still be able to see the middle layer. When the touchesEnded is called, copy the drawing onto layer 1, where it can stay. remove it from layer 3, and you should accomplish what you want.

Is that what you were thinking?

Upvotes: 1

Related Questions