2011-11-14 33 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

编辑

- (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