2

私はiPhone開発に不慣れです、

不規則な曲線がなく、滑らかでなければならないように直線を作ろうとしているアプリケーションを作成しています

コアグラフィックスを使用しましたが、機能していないようです。

スクリーンショット 私はUITouchを使ってこれらの線を赤い色でまっすぐに描きたいだけです。

openGlに行くべきですか?

はいの場合、どのように実装できますか。

  drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = viewField.frame;
[self.view addSubview:drawImage];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = NO;
UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
}

lastPoint = [touch locationInView:viewField];
//lastPoint.y = 20;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];    
CGPoint currentPoint = [touch locationInView:viewField];
//currentPoint.y -= 20; // only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(viewField.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;    
   }

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

  UITouch *touch = [touches anyObject];

  if ([touch tapCount] == 2) {
    drawImage.image = nil;
    return;
  }
  if(!mouseSwiped) {
    //if color == green
    UIGraphicsBeginImageContext(viewField.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width,   drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);   //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   }
   }

前もって感謝します !

4

1 に答える 1

0

あなたが投稿した画像から判断すると、アプローチを変えることをお勧めします。ダブルクリックして描画を開始します。次に、ポリゴン内のすべてのエッジポイントをタップします(= touchesStartedおよびtouchesEnded、touchesMovedではありません)。進むにつれて、これらのポイントの間に直線を描きます。ダブルクリックしてポリゴンを終了します。終点が始点に十分近い場合は、パスを自動的に閉じることを決定できます。

于 2012-11-23T19:59:03.713 に答える