Hi Folks,
You would have seen shapes, graphs in iPod, iPhone and iPad. Have you ever thought about the code behind it. Here it is. I am going to show simple code to draw shapes. In iPhone, its just like joining the co-ordinates. Here it goes.
Step 1: Create a ViewController in my case, its SampleViewController. And create a UIView in my case its just SampleView.
Step 2: In your AppDelegate implementation file create an instance for ViewController like this. Create the instance in didFinishLaunchingWithOptions delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the view controller’s view to the window and display.
SampleViewController *ControllerInstance = [[SampleViewController alloc] initWithNibName:@”SampleGraphViewController” bundle:nil];
[window addSubview:ControllerInstance.view];
[window makeKeyAndVisible];
return YES;
}
Step 3: Create instance of UIView in your ViewController implementation file.
- (void)viewDidLoad {
[super viewDidLoad];
SampleGraph *ViewInstance = [[SampleGraph alloc]initWithFrame:self.view.frame];
[self.view addSubview:ViewInstance];
[ViewInstance release];
}
Don’t forget to import the files. SampleView.h in SampleViewController.m and SampleViewController.h in AppDelegate.m
Step 4: Add the following code in SampleView.m in the delegate - (void)drawRect:(CGRect)rect as follows.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Draw a Vertical Line.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGContextMoveToPoint(context, 100.0,100.0);
CGContextAddLineToPoint(context, 100.0,500.0);
CGContextStrokePath(context);
//Draw a rectangle.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
//Filling color inside the rectangle.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, rectangle);
}
P.S : You can try this only if you have a Mac OS ie. Apple Computer. Mac mini costs around 45000 and even mouse cost around 2500. Hope you buy a new Mac and try this.
You would have seen shapes, graphs in iPod, iPhone and iPad. Have you ever thought about the code behind it. Here it is. I am going to show simple code to draw shapes. In iPhone, its just like joining the co-ordinates. Here it goes.
Step 1: Create a ViewController in my case, its SampleViewController. And create a UIView in my case its just SampleView.
Step 2: In your AppDelegate implementation file create an instance for ViewController like this. Create the instance in didFinishLaunchingWithOptions delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the view controller’s view to the window and display.
SampleViewController *ControllerInstance = [[SampleViewController alloc] initWithNibName:@”SampleGraphViewController” bundle:nil];
[window addSubview:ControllerInstance.view];
[window makeKeyAndVisible];
return YES;
}
Step 3: Create instance of UIView in your ViewController implementation file.
- (void)viewDidLoad {
[super viewDidLoad];
SampleGraph *ViewInstance = [[SampleGraph alloc]initWithFrame:self.view.frame];
[self.view addSubview:ViewInstance];
[ViewInstance release];
}
Don’t forget to import the files. SampleView.h in SampleViewController.m and SampleViewController.h in AppDelegate.m
Step 4: Add the following code in SampleView.m in the delegate - (void)drawRect:(CGRect)rect as follows.
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Draw a Vertical Line.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGContextMoveToPoint(context, 100.0,100.0);
CGContextAddLineToPoint(context, 100.0,500.0);
CGContextStrokePath(context);
//Draw a rectangle.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
//Filling color inside the rectangle.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor grayColor].CGColor);
CGContextSetLineWidth(context,10.0);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillRect(context, rectangle);
}
P.S : You can try this only if you have a Mac OS ie. Apple Computer. Mac mini costs around 45000 and even mouse cost around 2500. Hope you buy a new Mac and try this.

Comments
Post a Comment