0

私はアプリを作成していて、一部では手書きを実装しようとしています。imageViewを使用して書き込みを行いました。これは保存され、PDFとしてサーバーに送信されます。touch begin、move、endを実装し、contextAddLineToPointを使用して、ユーザーがコンテンツを作成するときに行を作成できます。でも。文章は少し先のとがったもので、ユーザーが文章の方向を変えたとき、つまり手紙を書いたときに曲線を作成しようとしています。手書き認識を実装するのではなく、描画される線を滑らかにするためです。

配列で構成される「バッファ」を作成しました。これは、ユーザーがタッチを動かしたときに2つの中間点を保持します。次のように:

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

UITouch *touch = [touches anyObject];

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

}

drawImageは、ところで書き込みに使用している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;
}

}

ご覧のとおり、2つのポイントが保存されるたびに、それらの間に線を引きます。これにより、描画が少し難しくなり、線がさらに不規則になります。

誰かが問題を解決できますか?私はiOSのグラフィックスに非常に慣れていないので、適切なAPIを使用しているかどうか、imageViewを使用する必要があるかどうかさえわかりません。

よろしくお願いします

4

1 に答える 1

1

PanGestureRecognizerと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」は、Appleサンプルコードの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);
    }    
    ..

お役に立てれば..

于 2012-04-13T00:24:08.903 に答える