1

を使用してiPad画面に直線を描画できるようにしたいUIBezierPath。これについてどうすればいいですか?

私がやりたいことは次のようなものです: 画面をダブルタップして開始点を定義します。指が画面の上に来ると、直線が指と一緒に移動します (これは、直線を作成するために次の指をどこに置くべきかを把握するために発生するはずです)。次に、画面をもう一度ダブルタップすると、終点が定義されます。

さらに、終点をダブルタップすると、新しい行が始まるはずです。

ガイダンスに使用できるリソースはありますか?

4

1 に答える 1

8
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startOfLine];
[path addLineToPoint:endOfLine];
[path stroke];

UIBezierPath クラス リファレンス

編集

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create an array to store line points
    self.linePoints = [NSMutableArray array];

    // Create double tap gesture recognizer
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [self.view addGestureRecognizer:doubleTap];
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateRecognized) {

        CGPoint touchPoint = [sender locationInView:sender.view];

        // If touch is within range of previous start/end points, use that point.
        for (NSValue *pointValue in linePoints) {
            CGPoint linePoint = [pointValue CGPointValue];
            CGFloat distanceFromTouch = sqrtf(powf((touchPoint.x - linePoint.x), 2) + powf((touchPoint.y - linePoint.y), 2));
            if (distanceFromTouch < MAX_TOUCH_DISTANCE) {    // Say, MAX_TOUCH_DISTANCE = 20.0f, for example...
                touchPoint = linePoint;
            }
        }

        // Draw the line:
        // If no start point yet specified...
        if (!currentPath) {
            currentPath = [UIBezierPath bezierPath];
            [currentPath moveToPoint:touchPoint];
        }

        // If start point already specified...
        else { 
            [currentPath addLineToPoint:touchPoint];
            [currentPath stroke];
            currentPath = nil;
        }

        // Hold onto this point
        [linePoints addObject:[NSValue valueWithCGPoint:touchPoint]];
    }
}

私は、金銭的補償なしに、マイノリティ レポート風のカメラ マジック コードを書いているわけではありません。

于 2011-11-14T19:48:51.173 に答える