2012-02-08 75 views
0

我正在创建一个应用程序,并且在一部分中,我正在尝试实现手写。我已经使用了一个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。

非常感谢

回答

1

如何使用Pan手势识别器和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”是你可以在苹果的样本中GLPaint例如找到PaintView实例码。该示例没有显示如何更改笔大小,但可以通过设置各种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); 
    }  
    .. 

希望这有助于..

+0

多德感谢了很多关于使用pangesture识别器的顶端!我永远不会想到将这两者混合在一起!还没有尝试过,但肯定看起来像它会工作:) – Septronic 2012-07-22 02:05:11

+0

是的,我试过了,曲线出来就像西瓜的曲线!! :)谢谢 – Septronic 2012-08-30 00:47:56