Draw a percentage of a circle - iphone

I would like to draw a circle like those in the picture in objective-c :
(source: hostingpics.net)
I just want to draw the blue circle filled with the corresponding percentage starting from the bottom. (NB : the range of the percentage is between 0 and 100) Don't worry about the tick in the center, it's just an imageView added over the circle view.

You could make 2 image views. One would contain your circle with the check mark, and the rest transparent. The other image would be below that, with a light blue rectangle. As the percentage increases, you raise the blue rectangle more and more by modifying it's frame. It would be visible of course through the transparent part of the image on top.
Hope this helps!

// Draw circle of radius r and center X,Y
CGRect r;
r.origin.y = Y-radius;
r.origin.x = X-radius;
r.size.width = 2*radius;
r.size.height 2*radius;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, r);
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor lightGray] CGColor]));
CGContextFillPath(ctx);
// Draw a blue color filled path. Below is half circle filled with blue color.
CGContextBeginPath(ctx);
CGContextAddArc(ctx, X, Y, r, -M_PI_2, M_PI_2, 1);
CGContextClosePath(ctx); // could be omitted
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextFillPath(ctx);
Idea is to draw a circle and fill it with light gray color and than draw and close path from arc of the circle (whatever angle is required) and fill it with blue color.

Adding to Levi's suggestion.
You can scale the rect image using CGAffineTransformScale.
e.g:
CGAffineTransform transform = self.rectImageView.transform;
float scale = 0.5;
self.rectImageView.transform = CGAffineTransformScale(transform, 1, scale);
This will scale the image to 50%
to set it back to 100%, just set scale = 2.

Related

CGContext giving unexpected line width

I'm trying to draw a L shape, the code works but the lines are not of the right width and is 5 pixels thick for vertical lines and some are 3 pixels thick despite me explicitly telling it to use 3 pixel as line width,what am I doing wrong? here's my code:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
CGContextAddPath(ctx, pathi);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 3.0);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetShouldAntialias(ctx, NO);
CGContextStrokePath(ctx);
I have already calculated path "pathi" The path is working fine.
The lines are actually not thick, the half of the lines gets cutoff while drawing.
You should apply padding equal to the half of the width( inn your case, 3/2 => 1.5) because the drawing always start from the mid of the points provided.

Improper borders in draw rect

I am trying to draw rounded corners on rectangle in drawRect method using bezier path, but somehow the rounded corner is being shown on inner side of rectangle, instead of both inner and outer sides. Code is given below. Also attached herewith is border that is currently being drawn (outer side of border is not rounded)
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context=UIGraphicsGetCurrentContext();
//Set gray color to whole context
[[UIColor lightGrayColor] set];
CGContextSetAlpha(context,0.7);
UIRectFill(rect);
// Configure the context colors and line
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:131./255. green:148./255. blue:219./255. alpha:1.0].CGColor);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(context, 10.0);
CGSize size=self.bounds.size;
CGFloat radius = 10.0;
CGSize guideSize=CGSizeMake(330, 130);
CGRect guideCoords= CGRectMake(size.width*.5-guideSize.width*.5, size.height*.5-guideSize.height*.5, guideSize.width , guideSize.height);
// Guide drawing
CGContextStrokeRect(context,guideCoords);
// Draw the Text
[kVinGuideMessage drawInRect:CGRectMake(guideCoords.origin.x+kSideMessageMargin, guideCoords.origin.y+guideSize.height+kMarginFromGuide, guideCoords.size.width-2*kSideMessageMargin,40) withFont:[UIFont systemFontOfSize:16.0] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];
//Get instersection and clear color of inner overlay
CGRect holeRectIntersection = CGRectIntersection(rect,guideCoords);
//----------ADDING ROUNDED CORNERS HERE-----------//
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:guideCoords cornerRadius:radius].CGPath;
CGContextAddPath(context, clippath);
CGContextClip(context);
//------------------------------------------------//
[[UIColor clearColor] setFill];
UIRectFill(holeRectIntersection);
}
The outer corners are being drawn by the CGContextStrokeRect(context,guideCoords); I think. At that point you haven't set a clipping path and your line width is 10 points, so why would the outer corners be rounded? I think you'll have better luck if you set a clipping path (probably not exactly the same clipping path you've got at the bottom) before calling stroke rect on the guideCoords rectangle.

Image based core graphics drawing

I have to draw many shapes like rectangle, circle, triangle etc.I am drawing them trough core graphics.
It is working fine.
I want to ask suppose I am having an image (square image of say 1024*1024), and now I want my circle to draw as image means like I draw red color circle or other color, so if it is possible to show my image as drawn in the circle, like the image is circle,
My code for normal circle drawing:-
- (void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [ [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1] CGColor]);
CGContextSetAlpha(ctx, 0.7);
CGContextMoveToPoint(ctx, radius, radius);
CGContextAddArc(ctx, radius,radius, radius, radians(0), radians(360), 0);
CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
I want to draw my image as circle.
Basically I want like we can fill colors in core graphics drawing whether circle, arc, triangle. so is that possible to fill my shape with my image instead of any solid colors.
Is this possible and if yes please suggest me how to achieve this.
Thanks in advance.
try this out for setting color in CGContextSetFillColorWithColor function [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"pattern.jpg"]CGColor];
Add a UIView with and rectangular on the image.
In this view, you must draw the hole.
Now i not have the mac and i cannot check, but try to substitute
CGContextSetFillColorWithColor(ctx, [ [UIColor colorWithRed:255.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1] CGColor]);
with
CGContextSetBlendMode(currentContext, kCGBlendModeClear);
let me know

CGContextDrawPath Stroke disappears if Fill is added

trying to draw circle divided to dynamically provided number of sectors. In the end my circle should look like pie.
I've created class Circle which contains number of Pies inside. Circle has method - (void)draw which calles [pie draw]; in for (Pie *pie in self.pies) loop.
Implementation of pie looks like this:
- (void)draw {
CGContextRef context = UIGraphicsGetCurrentContext();
float startAngleRads = degreesToRadians(_startAngle);
float endAngleRads = degreesToRadians(_endAngle);
CGContextBeginPath(context);
CGContextSetLineWidth(context, 1.0f);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGColorRef strokeColor = [_strokeColor CGColor];
CGContextSetStrokeColorWithColor(context, strokeColor);
CGColorRef fillColor = [_bgColor CGColor];
CGContextSetFillColorWithColor(context, fillColor);
CGContextMoveToPoint(context, _x, _y);
CGContextAddArc(context, _x, _y, _r, startAngleRads, endAngleRads, YES);
CGContextDrawPath(context, kCGPathStroke);
CGContextFlush(context);
}
This draws nice circle with sectors but without any filling of course. When I change last lines to CGContextDrawPath(context, kCGPathStrokeFill); my stroke lines disappears and only last line (last sector) stays at the screen, other parts of circle are filled with bgcolor and no stroke is visible anymore.
The question is: how can I draw completly separated "sectors" (every sector should look like triangle with Arc and Stroke and Fill). I need this because I want to toggle visibility of that sectors but at the moment I cant even figure out how to draw them correctly.
If this is important, all drawing happens on the layer with self.opaque = NO;.
Ok, found my error: had to set NO in CGContextAddArc call.

Drawing a rounded drop shadow with Quartz

I am trying to draw a drop shadow underneath an image to which I apply rounded corners, but I have 2 problems:
The drop shadow only appears underneath the non-rounded area of the rounded image and not underneath the bottom rounded corners as one would get if applying a drop shadow in photoshop.
Using the same settings as in photoshop (a 2 y-axis offset, 1 blur and 85% black) results in a much darker shadow which does not appear as blurred as it should.
Any help would be appreciated please.
float myColorValues[] = {0, 0, 0, 0.85};
CGColorRef myColor = CGColorCreate(colorSpace, myColorValues);
CGContextSetShadowWithColor(context, CGSizeMake(0, -2), 2, myColor);
// Draw a round corner path
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, 68, 68);
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(1, 2, 70, 70), imageScaledAndCropped);
The solution is to draw a rounded bezier path underneath the image and to apply the shadow to that.