Core Graphics is one of the most powerful framework of cocoa-touch.
Lots of animation,Drawings,Coloring etc features are provided in it.
Today in this blog I will explain how to start with core graphics, basically this blogs contributes how to draw different shapes and with different styles.
1.)Drawing a Line Segment:-
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextMoveToPoint(context, 50, 200);
CGContextAddLineToPoint(context,100,100);
CGContextMoveToPoint(context, 50, 200);
CGContextAddLineToPoint(context,200,400);
CGContextStrokePath(context);
2.)Drawing a Filled Circle
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 255, 0, 255, 0.9);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, 0.9);
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 50, 50));
3.)Drawing a Hollow Circle
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 255, 0, 255, 0.9);
CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, 0.9);
CGContextStrokeEllipseInRect(contextRef, CGRectMake(200, 100, 50, 50));
4.)Drawing a circle with boundary
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0, 255, 0, 1);
CGContextFillEllipseInRect(context, CGRectMake(100, 200, 25, 25));
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextStrokeEllipseInRect(context,CGRectMake(100, 200, 25, 25));
5.)Drawing a Hollow Rectangle
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 255, 255, 0, 1);
CGContextStrokeRect(ctx, CGRectMake(195, 195, 60, 60));
6.)Drawing a Solid Rectangle
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 255, 255, 0, 1);
CGContextFillRect(ctx, CGRectMake(260, 195, 60, 60));
7.)Drawing a Triangle
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1);
CGPoint points[6] = { CGPointMake(200, 200), CGPointMake(250, 250),
CGPointMake(250, 250), CGPointMake(100, 250),
CGPointMake(100, 250), CGPointMake(200, 200) };
CGContextStrokeLineSegments(ctx, points, 6);
![Screen shot 2011-07-18 at 11.39.23 AM.png](http://alpha.local/users/gypsaagarwal/weblog/490fa/images/c4fa2.png)
I hope this blog will help you in drawing these shapes with different styles.
Happy Coding:)
No comments:
Post a Comment