UIBezierPath没有绘制平滑的曲线

时间:2012-06-05 11:33:32

标签: ios ios5 core-graphics 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];    
}

这是图片

enter image description here

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

2 个答案:

答案 0 :(得分:4)

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

Smoothing a hand-drawn free shape

Smoothing a hand-drawn curve

根本不是iOS特有的。

答案 1 :(得分:4)

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

如果您需要任何浮动值

,请更改该值