Line Drawing on iPhone / iPad

I would like to develop an application where the user can draw lines ... but I do not want to draw straight lines, but I want to show the line how users draw it. When the user gets from point A to B, I would like to straighten the line (if users want it).

To do this, I want to change my view of the grid, starting from 0.0 (top left) and ending with 320,480 (for iPhone) and 768,1024 (for iPad) (bottom right).

For that matter, I have point A at 10.10 and point B at 100 100.

My question is:
- How to create this grid?
- How to create these points?
- How to draw this line without straightening it?
- How to draw a straightening line?

My problem is that I am familiar with creating β€œnormal” user interface applications. I am not familiar with open-GL ect.

Hope someone can help me.

Best wishes,
Paul Pilen

+8
ios iphone opengl-es grid
source share
2 answers

You subclass UIView and override the - (void)drawRect:(CGRect)rect .

In it you see the graphical context:

 CGContextRef context = UIGraphicsGetCurrentContext(); 

And you use this to make calls to Core Graphics, for example:

 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath (context); for (k = 0; k < count; k += 2) { CGContextMoveToPoint(context, s[k].x, s[k].y); CGContextAddLineToPoint(context, s[k+1].x, s[k+1].y); } CGContextStrokePath(context); 

See the quartz 2D programming guide for all details.

+17
source share

You can drag a straight line when the user drags it based on the start and end points, draw a line using UIBezierPath and CAShapeLayer:

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; startingPoint = [touch locationInView:baseHolderView]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; endingPoint = [touch locationInView:baseHolderView]; [self makeLineLayer:baseHolderView.layer lineFromPointA:startingPoint toPointB:endingPoint]; } -(void)makeLineLayer:(CALayer *)layer lineFromPointA:(CGPoint)pointA toPointB:(CGPoint)pointB { CAShapeLayer *line = [CAShapeLayer layer]; UIBezierPath *linePath=[UIBezierPath bezierPath]; [linePath moveToPoint: pointA]; [linePath addLineToPoint:pointB]; line.path=linePath.CGPath; line.fillColor = nil; line.opacity = 2.0; line.strokeColor = [UIColor blackColor].CGColor; [layer addSublayer:line]; } 

Hope this helps to achieve your goal.

0
source share

All Articles