如何在iPhone上用手指滑动/触摸动作绘制平滑线?

时间:2011-01-23 03:13:03

标签: iphone cocoa ipad draw

我有以下代码但不顺利。如果我画一个圆圈,我会看到尖角。

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();

3 个答案:

答案 0 :(得分:0)

您正在看到一些尖角,因为您绘制了太多短的抗锯齿线段,这些线段都需要超过1/60秒,因此您最终会丢失UI触摸事件更新,从而导致更加锯齿状路径。

iOS设备上没有加速2D CG绘图。

如果您想坚持使用CG绘图,请尝试仅绘制最后4个左右的线段,可能会关闭抗锯齿,并查看是否更顺畅。然后在路径绘制完成后填写其余部分(触摸)。

答案 1 :(得分:0)

即使有完整的60FPS,你也会得到边缘而不是曲线。使用CG最好的选择是使用bezier路径。在CG之外,splines。

答案 2 :(得分:0)

你需要做的是每次触摸移动时都不调用绘图函数,而是在每次调用时创建累加器并递增它。如果达到某个阈值,则执行绘图代码。但是你应该在第一次调用方法时运行它。要找到一个好的门槛,你必须试验它。

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++;
相关问题