【问题标题】:how to create curve for lines when handwriting手写时如何为线条创建曲线
【发布时间】:2012-02-08 19:37:37
【问题描述】:

我正在创建一个应用程序,其中一部分是我正在尝试实现手写。我使用了一个 imageView 来写,它将被保存并作为 pdf 发送到服务器。我已经实现了触摸开始、移动和结束,并且使用 contextAddLineToPoint,我可以在用户编写内容时创建线条。然而。文字有点尖锐,当用户改变书写方向时,我正在尝试创建曲线,即写信时。我真的不想实现手写识别,而只是为了平滑绘制的线条。

我创建了一个“缓冲区”,由一个数组组成,当用户移动触摸时,它会保存两个中间点。如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];

lastPoint = [touch locationInView:drawImage];
NSLog(@"first touch:%@",[NSValue valueWithCGPoint:lastPoint]);

}

drawImage 是我用来写在 btw 上的 imageView。然后去触摸感动:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];   
currentPoint = [touch locationInView:drawImage];

if (movementCount<2) {
    [pointHolderArray addObject:[NSValue valueWithCGPoint:currentPoint]];
    movementCount ++;
    NSLog(@"pointHolderArray: %@",pointHolderArray);
}else
{      
    NSLog(@"buffer full");
    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:drawImage.frame];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.3, 0.5, 0.2, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),[[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:0]CGPointValue].x,[[pointHolderArray objectAtIndex:0]CGPointValue].y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[pointHolderArray objectAtIndex:1]CGPointValue].x,[[pointHolderArray objectAtIndex:1]CGPointValue].y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = [touch previousLocationInView:drawImage];
    [pointHolderArray removeAllObjects];
    movementCount=0;
}

}

如您所见,每次存储两个点时,我都会在它们之间画一条线。这使得绘图稍微困难,线条更加参差不齐。

谁能帮助解决这个问题,我对 iOS 中的图形非常陌生,不确定我是否使用了正确的 API,以及是否应该使用 imageView。

提前感谢

【问题讨论】:

    标签: core-graphics handwriting cgcontextdrawimage


    【解决方案1】:

    如何使用 Pan Gesture Recognizer 和 GLPaint。你在 viewDidLoad 中设置了这样的东西:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIPanGestureRecognizer *g = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewPanned:)];
    
    ..
    

    然后,捕捉平移运动:

    -(void) viewPanned:(UIPanGestureRecognizer *)g 
    {
    
        CGPoint point = [g locationInView:paintView];
        // invert y for OpenGL
        point.y = paintView.bounds.size.height - point.y ;
    
        switch (g.state) {
            case UIGestureRecognizerStateBegan:
                prevPoint = point ;
                break ;
            case UIGestureRecognizerStateChanged:
                [paintView renderLineFromPoint:prevPoint toPoint:point] ;
                prevPoint = point ;
                break ;
            case UIGestureRecognizerStateEnded:
                prevPoint = point ;
                break ;
        }
    }
    

    此处显示的“paintView”是 PaintView 的实例,您可以在 Apple 示例代码的 GLPaint 示例中找到它。该示例没有显示如何更改画笔大小,但您可以通过设置各种 glPointSize 来实现。

    - (void)setBrushSize:(CGFloat)size 
    { 
      if( size <= 1.0 ) {
            glPointSize(10);
        }
        else if ( size <= 2.0 ) {
            glPointSize(20);
        }
        else if ( size <= 3.0 ) {
            glPointSize(30);
        }    
        else if ( size <= 4.0 ) {
            glPointSize(40);
        }    
        ..
    

    希望这会有所帮助..

    【讨论】:

    • 老兄非常感谢有关使用手势识别器的提示!我从没想过把这两者混在一起!!还没有尝试过,但看起来肯定会起作用:)
    • 是的,我试过了,曲线就像西瓜的曲线一样!!:) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多