2011-11-14 25 views
1

UIBezierPathを使ってiPadの画面に直線を描きたいと思っています。私はこれについてどうやって行くのですか?UIBezierPathを使って滑らかな直線を描く方法は?

私がしたいことは、次のようなものです。開始点を定義するために画面をダブルタップします。私の指が画面の上にくると、私の指で直線が動くようになります(これは、次の指をどこに置いて直線を作り出すかを決めるために起こります)。次に、画面をダブルタップすると、終点が定義されます。

また、エンドポイントをダブルタップすると新しい行が始まるはずです。

私が指導に利用できるリソースはありますか?

+2

ダウン投票するときは何らかの説明をするのが普通です。 – ryanprayogo

+0

@raazあなたがガラスに触れなくなったら、あなたの指を追跡することはできません。あなたがマイノリティ・レポート・エスケープ・マジックをカメラで実装しない限り、まあ、それは大したことではないと思います。残りの部分は簡単に達成できます。「UITouch」情報を@ StuDevの答え(UIBezierPathのポイント)に入れるだけです。 –

答えて

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

UIBezierPath Class Reference

EDIT

- (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]]; 
    } 
} 

私は金銭的補償なしマイノリティ・リポート風のカメラ魔法のコードを書いていませんよ。

+0

@ studdev、私は何をしたいの詳細な説明を追加しました – raaz

関連する問題