Question

I have this code

CGPoint one = [button center];
CGPoint two = [button center];

How draw a line at this points? thanks

I try this

CGContextRef ctx = UIGraphicsGetCurrentContext();



CGContextMoveToPoint(ctx, one.x, one.y);
CGContextAddLineToPoint(ctx, two.x, two.y);
CGContextStrokePath(ctx);

But i have this error:

<Error>: CGContextDrawPath: invalid context 0x0
Was it helpful?

Solution

There are two main ways to draw your line.

One way is to create a subclass of UIView and override its drawRect: method. This is described in “The View Drawing Cycle” in the Drawing and Printing Guide for iOS.

The other way is to use a CAShapeLayer. There are lots of answers on stackoverflow about using a shape layer. Check out this answer for a start.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top