UIBezierPath光滑曲线用于手指触摸绘图

3
我想在手指触摸绘制线条时使曲线更加平滑,并且我希望在UIBezierPath中获得解决方案,只针对我的代码进行线条平滑处理。我的代码如下:
@implementation MyLineDrawingView
@synthesize undoSteps;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        [super setBackgroundColor:[UIColor whiteColor]];

        pathArray=[[NSMutableArray alloc]init];
        bufferArray=[[NSMutableArray alloc]init];



    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{

    [[UIColor blackColor] setStroke];
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];    
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [bufferArray removeAllObjects];
    myPath=[[UIBezierPath alloc]init];
    myPath.lineWidth=5;
    myPath.miterLimit=-10;
    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];

}

-(void)undoButtonClicked
{
    if([pathArray count]>0)
    {
    UIBezierPath *_path=[pathArray lastObject];
    [bufferArray addObject:_path];
    [pathArray removeLastObject];
    [self setNeedsDisplay];
    }

}
-(void)redoButtonClicked
{
    if([bufferArray count]>0){
        UIBezierPath *_path=[bufferArray lastObject];
        [pathArray addObject:_path];
        [bufferArray removeLastObject];
        [self setNeedsDisplay];
    }   
}
- (void)dealloc
{
    [pathArray release];
    [bufferArray release];
    [super dealloc];
}

@end

我使用我的代码获取输出,如下所示的屏幕截图:

enter image description here

我想要像下面这个屏幕截图一样平滑的曲线输出:

enter image description here

有人可以帮我吗?非常感谢!

提前致谢!

1个回答

5
问题在于你只使用addLineToPoint:来添加路径上的点。这等同于创建一系列直线。你真正需要添加控制点,就像在Illustrator或Sketch中绘制曲线路径时拖动的手柄一样。
你可以使用addCurveToPoint:controlPoint1:controlPoint2:来添加这些控制点;关键是要确定相对于从touches*事件实际获取的点放置控制点的位置。
关于可能的技术的讨论,我推荐阅读以下问题:绘制平滑曲线 - 需要的方法

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接