开发者

How do I draw a smooth line with a finger slide/touch motion on the iPhone?

I have the following code but it is not smooth. If I draw a circle, i see the sharp corners.

UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLin开发者_运维技巧eToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


You are seeing some of the sharp corners because you are drawing too many short antialiased line segments, which all takes more than 1/60th of a second, thus you end up missing UI touch event updates, which leads to a more jaggy path.

2D CG drawing is not accelerated on iOS devices.

If you want to stick with CG drawing, try drawing only the last 4 or so line segments, maybe turn off antialiasing, and see if that's smoother. Then fill in the rest after the path drawing is done (touch up).


Even with a full 60FPS you'll get edges instead of a curve. Your best bet with CG is to use bezier paths. Outside of CG, splines.


what you need to do is not call the drawing functions every time the touch moves, instead make an accumulator and increment it every time it's called. if it reaches some threshold then you do the drawing code. but you should definitely run it the first time the method is called. to find a good threshold you must experiment with it.

static int accum = 0;
if ((accum == 0) || (accum == threshold)) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
accum = 0;
}
accum++;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜