3
votes

I am using UIBezierPath for drawing, and I have written the code on touch events and its working fine, but my curves are not smooth, when I am move my finger around and draw some curve, they are not smooth.

- (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];    
}

Here is the image

enter image description here

In the above image if you see the letters a and d you will see that the curve is not smooth. What should I do, to get a smooth curve?

2

2 Answers

4
votes

Well you are using addLinePoint so obviously you get lines, not curves. You would be interested in other methods to give you smooth curves: addCurveToPoint or addQuadCurveToPoint. But as you can see from the API, besides the points you are actually drawing with your finger, you need also control points which do not come free with the drawing. Even Photoshop is asking you to move them around when doing curvature. In other words, making your hand drawing smooth "automagically" involves quite some mathematics. Google "smoothing hand drawn" and you will get at least these points to start with

Smoothing a hand-drawn free shape

Smoothing a hand-drawn curve

It is really not iOS specific at all.

3
votes

Just use this line. it will solve your problem myPath.miterLimit=-10;.

Change the value if you need to any thing it takes float value