开发者

CGContextStrokePath() causing "dots" in realtime drawing (finger painting) app

开发者 https://www.devze.com 2023-03-10 04:04 出处:网络
I\'m trying to write a finger painting type app.I am starting a path in touchesBegan and adding to that path in touchesMoved.In touchesMoved, I use the following code:

I'm trying to write a finger painting type app. I am starting a path in touchesBegan and adding to that path in touchesMoved. In touchesMoved, I use the following code:

CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);

CGContextStrokePath(context);

I call CGContextStokePath so that the path shows up in realtime as the user draws. The problem is that when using low alpha values, I get dots between the successive path segments where the end cap is essentially drawn twice - once for the previous segment, and once for the current segment.

I've tried using different line caps but the result isn't very pretty. I've also tried using the CGC开发者_运维百科ontextDrawPath function with all the various constants and I get the same result.

You can see the results here: http://www.idea-asylum.com/pathwithdots/index.html - It shows a line with alpha = 1.0 and one with alpha = 0.2.

Any ideas? Thanks in advance!


First, I hope you're drawing each shape into a separate layer (and I don't mean CALayer, I mean an internal construct unique to your app). This not only simplifies this task, it makes undo more or less painless (just move the last/topmost layer into a different array and hide it, and empty that array when the user draws a new layer).

Second, during the construction of the shape, don't only remember the last point. Create a CGMutablePath when the user begins the shape and add each subsequent point as another lineto. This also lets you keep the path around in that layer, which means you can throw the rendered image away if a low-memory warning arrives and re-create it the next time you need it.

Third, each time you update the shape during its creation, get its area so far, invalidate that section, and redraw all the layers under it as well as the shape being drawn (as it exists so far). That is, redraw the background, clobbering the new shape, and then draw the up-to-date version of the new shape on top.

Once you are constructing the shape as a single path, and stroking that single path in each draw cycle, the intersections between segments will disappear.

0

精彩评论

暂无评论...
验证码 换一张
取 消