开发者

Draw a smooth line with finger touch on iPhone

开发者 https://www.devze.com 2022-12-28 02:51 出处:网络
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.It makes corners/turns for example when I try to do a circle.I think I have to

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. It makes corners/turns for example when I try to do a circle. I think I have to use OpenGL. Any ideas? sample code? tutorials?

Thanks!

UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGCon开发者_如何学运维textSetLineCap(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();


I ended up using Open GL and a particle. Good example to check out for this is Apple's GLPaint example code.


Add this to your code to smooth out the lines between points

CGContextSetLineJoin(UIGraphicsGetCurrentContext(), kCGLineJoinRound);

That'll make a huge difference


You don't need OpenGL. Just keep a reference to the start point and clear the context in touchesMoved, and do something along the lines of:

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

This will make a straight line from the start point to the current point.

0

精彩评论

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

关注公众号