2012-02-08 65 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