2011-11-14 4 views
1

UIBezierPath을 사용하여 iPad 화면에서 직선을 그릴 수 있기를 원합니다. 나는 이것에 대해 어떻게 갈 것인가?UIBezierPath를 사용하여 부드러운 직선을 그리는 방법?

내가 원하는 것은 다음과 같습니다. 시작점을 정의하기 위해 화면을 두 번 탭합니다. 손가락이 화면 위에 오면 직선이 손가락으로 움직입니다 (직선을 만들 수 있도록 다음 손가락을 놓아야하는 위치를 파악해야합니다). 그런 다음 화면을 두 번 탭하면 끝점이 정의됩니다.

또한 끝점을 두 번 탭하면 새 줄이 시작됩니다.

지도 용으로 사용할 수있는 리소스가 있습니까?

+2

아래로 투표 할 때 일종의 설명을하는 것이 일반적입니다. – ryanprayogo

+0

@raaz 사용자의 손가락이 유리에 닿지 않으면 추적 할 수 없습니다. 글쎄, 당신이 카메라로 몇 가지 마이너리티 리포트 - 에스 케야 마술을 구현하지 않는 한,하지만 그것은 거의 기념비적 인 노력 가치가 있다고 생각합니다. 나머지는 쉽게 달성 할 수 있습니다 : @ UTouch의 정보 (UIBezierPath의 포인트)에 'UITouch'정보를 퍼널하십시오. –

답변

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

관련 문제