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.

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:

-(void)applicationDidFinishLaunching:(UIApplication *)application {

CircleView *view = [[CircleView alloc] initWithFrame:[window frame]];
[window addSubview:view];
[view release];

[window makeKeyAndVisible];

}

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

- (void)drawRect:(CGRect)rect {
CGContextRef contextRef = UIGraphicsGetCurrentContext();

CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5);

// Draw a circle (filled)
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));

// Draw a circle (border only)
CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25));

// Get the graphics context and clear it
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);

// Draw a green solid circle
CGContextSetRGBFillColor(ctx, 0, 255, 0, 1);
CGContextFillEllipseInRect(ctx, CGRectMake(100, 100, 25, 25));

// Draw a yellow hollow rectangle
CGContextSetRGBStrokeColor(ctx, 255, 255, 0, 1);
CGContextStrokeRect(ctx, CGRectMake(195, 195, 60, 60));

// Draw a purple triangle with using lines
CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1);
CGPoint points[6] = { CGPointMake(100, 200), CGPointMake(150, 250),
CGPointMake(150, 250), CGPointMake(50, 250),
CGPointMake(50, 250), CGPointMake(100, 200) };
CGContextStrokeLineSegments(ctx, points, 6);

}

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

You can Download SourceCode from here ThreeViews

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

4 Responses to “Draw Circle Triangle and Rectangle in iPad”

  1. Frank says:

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

    #import “CircleView.h”

  2. Sushant says:

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

  3. jithin says:

    It shows an error like ‘CircleView’ undeclared….

  4. vishy says:

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

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG