2011-01-23 59 views
0

我有下面的代码,但它不光滑。如果我画一个圆圈,我会看到尖角。如何在iPhone上用手指滑动/触摸动作绘制平滑线?

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

只是一个快速的音符......你可能想取代你UIGraphicsGetCurrentContext与UIGraphicsBeginImageContext之后一个电话呼叫,并将结果存储在一个变量。没有必要为每一行调用UIGraphicsGetCurrentContext,效率低下且难以阅读。 – v01d 2011-01-23 10:11:58

回答

0

您已经看到了一些尖角的,因为你是画过许多短抗锯齿线段,这一切都需要超过1/60秒,所以你最终错过UI触摸事件的更新,这会导致走到一条更粗糙的路上。

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

如果你想坚持CG绘画,试着画只有最后4个左右的线段,也许关掉抗锯齿,看看这是平滑。然后在路径绘制完成后填写剩下的部分(触摸)。

0

即使有一个完整的60FPS,你会得到的边缘,而不是曲线。 CG最好的选择就是使用贝塞尔路径。 CG之外,样条线。

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++; 
+0

如果这种方法不够灵敏,可以根据currentPoint和lastPoint之间的距离动态更改阈值,则阈值应与差值成反比。 – Rich 2011-01-23 22:27:24