1

I've subclassed a UIView and overridden the touchesBegan, touchesMoved, touchesEnded, and drawRect methods to create an app that allows the user to draw by touching the screen. I'm using the Quartz 2D library to do the drawings.

In touchesBegan, touchesMoved, and touchesEnded, I keep track of the current and previous locations of the touch event. Each time the touch moves and when the touch ends, I call setNeedsDisplayInRect using the smallest rectangle that contains the previous and current location of the touch in order to preserve underlying drawings. (I want all drawings to add on to one another like layers.)

I've noticed a strange artifact: When creating a drawing that overlaps with another drawing, Quartz re-draws the rectangle passed into setNeedsDisplayInRect but erases everything below that rectangle.

I suspect that the issue is due to the blending mode, however I experimented with a number of different blending modes and none seemed to do the trick.

How do I draw a path that preserves the underlying content?

4

1 回答 1

1

Quartz 不是画布模型。它不跟踪以前循环中绘制的内容。每次drawRect:调用时,您有责任处理您传递的矩形中的每个像素。默认情况下(设置在 中UIView clearsContextBeforeDrawing),Quartz 会为您清除矩形,但每次都绘制内容是您的工作。

如果要对事物进行分层,则需要将每个事物放在自己的CALayerUIView中,或者每次drawRect:调用时都需要重绘任何重叠。

于 2012-04-08T20:12:45.680 回答