UIBezierPath没有绘制平滑曲线

我正在使用UIBezierPath绘图,我已经编写了关于触摸事件的代码并且它的工作正常,但是我的曲线并不平滑,当我移动手指并绘制一些曲线时,它们并不平滑。

- (void)drawRect:(CGRect)rect { [[UIColor redColor] setStroke]; for (UIBezierPath *_path in pathArray) [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; } #pragma mark - Touch Methods -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { myPath=[[UIBezierPath alloc]init]; myPath.lineWidth=5; myPath.lineCapStyle = kCGLineCapRound; myPath.flatness = 0.0; UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; [myPath moveToPoint:[mytouch locationInView:self]]; [pathArray addObject:myPath]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; [myPath addLineToPoint:[mytouch locationInView:self]]; [self setNeedsDisplay]; } 

这是图像

在此处输入图像描述

在上图中,如果看到字母a和d,则会看到曲线不平滑。 我该怎么做才能获得平滑的曲线?

只需使用此行。 它会解决你的问题myPath.miterLimit=-10;

如果您需要任何浮动值的东西,请更改该值

那么你正在使用addLinePoint所以显然你得到的是行,而不是曲线。 您会对其他方法感兴趣,为您提供平滑的曲线: addCurveToPoint或addQuadCurveToPoint 。 但是正如您从API中看到的那样,除了您实际用手指绘制的点之外,您还需要控制点,这些点不会随绘图而自由。 甚至Photoshop也会要求你在弯曲时移动它们。 换句话说,让你的手绘画“自动”平滑涉及相当多的数学。 谷歌“平滑手绘”,你将至少得到这些点

平滑手绘自由形状

平滑手绘曲线

它根本不是iOS特有的。