Reputation: 69
How can we draw a line automatically after the user taps two different points. The line should be joining those two different points.
What frameworks and methods should be used do so.
Thanks
Upvotes: 2
Views: 6683
Reputation: 1867
You can store the touched locations in two different CGPoint
with the help of the touchedEnded
method (documentation).
Then, when you have your two points, you can add a new UIView as subview which is aware of the two CGPoint
and will draw a line in its drawRect
method. Or do it in the current view, by calling [view setNeedsDisplay]
to trigger its own drawRect
method.
If you don't know how to draw a simple line with CoreGraphics, here is the beginning :
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor]CGColor]);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, startPoint.x, startPoint.y);
CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
Upvotes: 5