Draw straight lines from Finger to iPhone

Background: I am trying to create a really simple iPhone application that will allow the user to draw a few straight lines on the screen with his finger.

I use these two methods in my UIViewController to capture the coordinates of each endpoint of a line.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

Question: I would like the line to be displayed as soon as touchesEnded fired, and then continued to draw more lines on the screen. How can I do it? I don't necessarily need the code, but I need help presenting a big picture on how to put it together. Also, I'm not a big fan of xibs and like to do something all programmatically if that affects the answer.

What I tried: I tried using Quartz 2d, but it seems that for this you need to make your drawing in the drawRect method of a separate subclass. So, would I need to create a new view for each row? and then my coordinates would be corrupted by b / c, I would have to translate the touches positions from the UIViewController into the view.

I also tried using OpenGL, with which I had a bit more success (using the GLPaint sample as a template), but OpenGL seems redundant to just draw some straight lines on the screen.

+4
source share
3 answers

You do not need multiple views, and you do not need OpenGL.

Subclass UIView - Call it CanvasView .

Make an object to represent the "string" in your canvas - this will be a subclass of NSObject with CGPoint properties for start and end.

CanvasView should contain an array of lines located on the canvas.

In -[CanvasView drawRect:] swipe through the lines in the array and draw each.

In -[CanvasView touchesBegan:withEvent:] write the starting point in the instance variable. In -[CanvasView touchesEnded:withEvent:] create a new row with start and end points and add it to your string array. Call [self setNeedsDisplay] to invoke a view view.

+2
source

U mentioned that you just need a big picture of the idea.

So here it is ..

Take a view, a subclass of UIView. Capture affects events and then draws it in drawRect: method ..

This is all that @kurtRevis mentioned in his answer.

Now, to avoid slowing down your drawing, save a standalone image that retains an updated view of the screen, and then just add lines above it. Thus, you will not slow down when there is a large array of lines that need to be drawn.

I hope you catch me.

+2
source

As far as I know, you can also use Core Graphics to draw a line, and from your question you do not need to create views for each individual line, instead, your single graphic view context will be a drawing sheet for all the drawing, and you are almost closer to the solution. Just using the touch coordinate, you can draw lines on the screen.
CGPoint previousPoint; // This must have a global scope, it will be used in all touch events

  - (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
 {       
    UITouch * touch = [touches anyObject];
    previousPoint = [touch locationInView: self];  // take the starting touch point;
 }
  - (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event 
 {
    CGPoint currentPoint;
    UITouch * touch = [touches anyObject];   
    currentPoint = [touch locationInView: self]; 
    CGContextRef context = UIGraphicsGetCurrentContext ();
    CGContextSetLineWidth (context, 2.0);
    CGContextMoveToPoint (context, previousPoint.x, previousPoint.y);
    CGContextAddLineToPoint (context, currentPoint.x, currentPoint.y);
    CGContextStrokePath (bluecontext);

 }

Hope this helps you, Let me know any problem ....

-1
source

Source: https://habr.com/ru/post/1402168/


All Articles