Deepranshu
Deepranshu

Reputation: 69

how to draw automatic line between two tap points in a view in iphone

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

Answers (2)

NSZombie
NSZombie

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

Sumanth
Sumanth

Reputation: 4921

You should use UIBezierPath for this. It can draw line curves if you give points the official documentation is here

check here as well

Upvotes: 0

Related Questions