Draw Circle Triangle And Rectangle in iPad

This is the very simple tutorial. In this tutorial we will see how to draw Circle, Triangle and  Rectangle in the iPad.

Step 1: Create a Window base application using template. Give the application name “ThreeViews”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add  one UIView file in the project. Select classes-> New File -> Add ->Cocoa Touch Class -> Select Objective C class -> Subclass of -> UIView. Give the class name “CircleView”.

Step 4: Open the ThreeViewsAppDelegate.m file and make the following changes in the file:

 

 

 

Step 5: Open the CicleView.m file and make the following changes in the drawRect: method.

 

 

 

Step 6: Now compile and run the application in the Simulator.

You can Download SourceCode from here ThreeViews

Leave a Comment:

10 comments
Frank says July 21, 2010

to have the code running you have to add the following in ThreeViewsAppDelegate.h:

#import “CircleView.h”

Reply
Sushant says July 21, 2010

No need to import CircleView.h file in the ThreeViewsAppDelegate.h file, only define in the ThreeViewsAppDelegate.m file.

Reply
Jeef says February 4, 2011

What if you want to draw lines but you don’t want to do it from the drawRect method?

Reply
jithin says March 7, 2011

It shows an error like ‘CircleView’ undeclared….

Reply
jithin says March 7, 2011

It shows an error like ‘CircleView’ undeclared….

Reply
vishy says April 13, 2011

Can you please try to download the Source Code and Compile it again

Reply
Carol says September 13, 2011

> CGContextRef ctx = UIGraphicsGetCurrentContext();

I get no warning, no errors… but CGContextRef is always NULL.
Why?

Also,
Why are both of these needed, instead of just 1:
> CGContextRef contextRef = UIGraphicsGetCurrentContext();
> CGContextRef ctx = UIGraphicsGetCurrentContext();
Do they point to 2 different things?

Reply
Groumfy69 says November 14, 2011

You’d better use CGContextAddArc() with startAngle=0 and endAngle=2*pi if you want a real round circle. Sample code:

CGContextAddArc(contextRef,width,width,width-2,0,2*3.1415926535898,1);
CGContextDrawPath(contextRef,kCGPathStroke);

Reply
Vineesh says February 27, 2012

How can I draw shapes by using user input.

Reply
nope says June 18, 2012

This does not work. What calls the rect function?

Reply
Add Your Reply