简体   繁体   中英

UIBezierPath not drawing a smooth curve

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

在此输入图像描述

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM