-1

I'm trying to implement a freehand drawing tool by which user can draw several shapes dynamically like rectangle,eclipse,circle etc on a PDF using touch begins and touch move methods. So anyone there who have done this kind of tool or have knowledge how to accomplish this,please help me out.

Thank you in advance.

Amir Khan
  • 1,318
  • 1
  • 14
  • 39
  • 2
    This question is far too broad for Stack Overflow. However, you'll perhaps want to start by implementing a [UIGestureRecognizer](https://developer.apple.com/reference/uikit/uigesturerecognizer) in order to capture where the user is touching the screen. – Tibrogargan Sep 19 '16 at 04:52
  • 1. Use UIGestureRecognier to detect user interaction 2. Create UIBezierPath to save points, then draw it on view by override drawInRect method – lee5783 Sep 19 '16 at 04:55
  • Thank you so much for your suggestion. Stack Overflow is the only platform for developers where actual help is being given. That's why I have asked this question. Actually I want to implement a tool look-alike of 'FieldWire' app.So You can suggest me some links of tutorials, Sample Projects if you know about it. @Tibrogargan :) – Amir Khan Sep 19 '16 at 07:34

1 Answers1

0

This is a very broad question to be answered on StackOverflow. Still I am trying to give you a start here.

1) Declare two global variables BOOL mouseSwiped, CGPoint lastPoint in your UIViewController.

2)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
}

3)

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    UIGraphicsBeginImageContext(yourSubview.frame.size);
    [yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3.0 );
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    yourSubview = UIGraphicsGetImageFromCurrentImageContext();
    [yourSubview setAlpha:1.0];
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;
}

4)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIGraphicsBeginImageContext(yourSubview.frame.size);
    [yourSubview drawInRect:CGRectMake(0, 0, yourSubview.frame.size.width, yourSubview) blendMode:kCGBlendModeNormal alpha:1.0];
    yourSubview = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

Hope this helps

Vamshi Krishna
  • 979
  • 9
  • 19