Can I draw shapes like circle, rectangle, line etc outside drawRect
method using
CGContextRef contextRef = UIGraphicsGetCurrentContext();
or is it mandatory to use it inside drawRect
only.
Please help me, let me know how can I draw shapes outside drawRect
method.
Actually i want to go on plotting dots on touchesMoved
event.
This is my code for drawing a dot.
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 0, 255, 0, 1);
CGContextFillEllipseInRect(contextRef, CGRectMake(theMovedPoint.x, theMovedPoint.y, 8, 8));
For Swift 4
Basically you need a context to draw something. You can assume context as a white paper.
UIGraphicsGetCurrentContext
will returnnull
if you are not in a valid context.IndrawRect
you get the context of the view.Having said that, you can draw outside
drawRect
Method. You can begin an imageContext to draw things and add it to your view.Look at the below example taken from here,